Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中运行linux命令以发送带引号的电子邮件_Java_Linux - Fatal编程技术网

在java中运行linux命令以发送带引号的电子邮件

在java中运行linux命令以发送带引号的电子邮件,java,linux,Java,Linux,我可以通过命令行在Linux中发送电子邮件: cat < x.txt | mail -s "SUBJECT" email@email.com 嗯,它不工作,我得到以下错误 Exception in thread "main" java.io.IOException: Cannot run program "cat < x.txt |": error=2, Datei oder Verzeichnis nicht gefunden at java.lang.Proce

我可以通过命令行在Linux中发送电子邮件:

cat < x.txt | mail -s "SUBJECT" email@email.com
嗯,它不工作,我得到以下错误

Exception in thread "main" java.io.IOException: Cannot run program "cat < x.txt |": error=2, Datei oder Verzeichnis nicht gefunden
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:620)
        at java.lang.Runtime.exec(Runtime.java:485)
        at test.main(test.java:9) Caused by: java.io.IOException: error=2, Datei oder Verzeichnis nicht gefunden
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
        at java.lang.ProcessImpl.start(ProcessImpl.java:134)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
        ... 3 more
线程“main”java.io.IOException中的异常:无法运行程序“cat 我是否将这些命令拆分错误


如何正确执行此命令?

您可能应该执行以下操作:

Process p = Runtime.getRuntime().exec("/usr/sbin/sendmail .. email@email.com");
OutputStream os = p.getOutputStream();
InputStream is = new FileInputStream("x.txt");
// copy the contents
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) != -1) {
    os.write(buf, 0, len); // only write as many as you have read
}
os.close();
is.close();

另外请注意,您已经有了javamail api,因此请确保通过使用这些库使您的代码平台独立。

您可能应该执行以下操作:

Process p = Runtime.getRuntime().exec("/usr/sbin/sendmail .. email@email.com");
OutputStream os = p.getOutputStream();
InputStream is = new FileInputStream("x.txt");
// copy the contents
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) != -1) {
    os.write(buf, 0, len); // only write as many as you have read
}
os.close();
is.close();

另外请注意,您已经有了javamail api,因此请确保使用这些库使代码平台独立。

尝试编写脚本并执行它,而不是执行单独的命令:

String email = "email@email.com"; // Or any way of entering the email address
String[] command = {"/bin/sh", "-c", "cat < x.txt | mail -s SUBJECT" +  email};
Process p = Runtime.getRuntime().exec(command);
String电子邮件=”email@email.com"; // 或者以任何方式输入电子邮件地址
String[]command={/bin/sh',“-c”,“cat

管道(
|
)是一个内置的shell,而不是实际的unix命令。

尝试编写脚本并执行它,而不是执行单独的命令:

String email = "email@email.com"; // Or any way of entering the email address
String[] command = {"/bin/sh", "-c", "cat < x.txt | mail -s SUBJECT" +  email};
Process p = Runtime.getRuntime().exec(command);
String电子邮件=”email@email.com"; // 或者以任何方式输入电子邮件地址
String[]command={/bin/sh',“-c”,“cat

Pipe(
|
)是一个内置的shell,而不是实际的unix命令。

好的,谢谢您的提示。不幸的是,我需要从java以param的形式传递电子邮件。有机会这样做吗?很抱歉,我没有更改代码以适合您的示例。更新了答案,没人这么说。我问的是任何机会,而不是代码。一个暗示就足够了。我找到了另一个解决方案,没有管道。无论如何,谢谢你,你睁开了我的眼睛!:)很抱歉,我不熟悉linux和那些命令。好的,谢谢你的提示。不幸的是,我需要从java以param的形式传递电子邮件。有机会这样做吗?很抱歉,我没有更改代码以适合您的示例。更新了答案,没人这么说。我问的是任何机会,而不是代码。一个暗示就足够了。我找到了另一个解决方案,没有管道。无论如何,谢谢你,你睁开了我的眼睛!:)很抱歉,我不熟悉linux和那些命令。为什么“cat