问题描述
我尝试在Linux和Java中使用mutt发送电子邮件,如果我从linux命令行执行mutt命令,则电子邮件发送效果很好
echo "test" | mutt -s "subject" -- "jojo@foo.com
现在,我有一个简单的Java应用程序,尝试执行相同的命令,却一无所获,甚至没有出错:
java -cp runtime-SNAPSHOT.jar MyApp "echo \"test\" | mutt -s \"subject\" \"jojo@foo.com\""
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class MyApp {
public static void main(String[] args) throws InterruptedException, IOException {
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try
{
String[] cmd = new String[3];
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + args[0] );
Process proc = rt.exec(args[0]);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
这是怎么了
1楼
您不会出错,因为echo似乎在您的系统上可用(通常为“ / bin / echo”)。 Runtime exec方法中的Stringtokenizer将行的其余部分作为参数传递给/ bin / echo,如下所示:
/bin/echo "\"test\"" "|" "mutt" "-s" "\"subject\"" "--" "\"jojo@foo.com\""
好吧,这是一个有效的命令,因为它调用/ bin / echo,并且/ bin / echo输出所有参数,但从不调用mutt。 (顺便说一句,/ bin / echo与内置的Bash shell中使用的回声是不同的,其行为略有不同...)
他们(Java)在exec方法中标记命令有时可能很方便,但会导致类似这样的令人讨厌的效果,因为它使人认为某些事情应该起作用,但实际上在这种情况下不起作用...
您可能想要的是一个执行命令行的shell。 因此,您必须实际执行一个shell(我在文件中标记了更改):
public class MyApp {
static class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(type + ">" + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException, IOException {
/*if (args.length < 1) {
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}*/
args = new String[]{"echo \"test\" | grep -i \"s\" " };
try {
String[] cmd = new String[3];
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + args[0]);
//Change here: execute a shell with the command line instead of echo:
Process proc = rt.exec(new String[]{"/bin/sh","-c", args[0]});
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
边注。 为了获得更好的最小测试用例:
- 我用一些grep替换了您的mutt命令,因为我不想发送邮件;)
我通过以编程方式创建array(“ args”)伪造了Java命令行。
使您的StreamGobbler静态以使其具有一个文件。
所有这些都不应该改变您的测试用例。 有所不同的是执行shell而不是/ bin / echo的rt.exec调用
示例运行:
Execing echo "test" | grep -i "s"
ExitValue: 0
OUTPUT>test