我有一种情况,我想(需要)在执行给定的 java 程序时通过电子邮件发送文件。复制命令在 Windows 和 Linux 上都可以正常执行,但是,当在 java 上命令执行时,mutt 不起作用。命令字符串生成得完美无缺(我目前手动键入打印到控制台的字符串以手动发送电子邮件),但是 Runtime.getRuntime.exec() 未正确执行它。
if (OS.indexOf("win") >= 0)
{
// windows
cmd = "cmd /C copy "+workbookFileName+" "+latestWorkbookFileName;
System.out.println("executing command windows: "+cmd);
Runtime.getRuntime().exec(cmd);
}
else
{
// others
cmd = "cp "+workbookFileName+" "+latestWorkbookFileName;
System.out.println("executing Linux: "+cmd);
Runtime.getRuntime().exec(cmd);
cmd = "echo \"This is the message body\" | mutt -a \""+workbookFileName+"\" -s \"message subject\" -- sqlinjection@domain.com";
System.out.println("executing Linux: "+cmd);
Runtime.getRuntime().exec(cmd);
System.out.println("bye");
}
最佳答案
这是一个关于如何使用 Runtime.exec
的很好的教程 When Runtime.exec() won't .
这个想法是,您需要使用由 Runtime.exec
创建的进程的标准输入、输出和错误,而我提到的站点是完美的。
关于java - 在 java shell 上执行 Linux 命令 (mutt) 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18695328/