首页 > 解决方案 > 从 Java Runtime.getRuntime 执行 linux mutt 不发送邮件也不给出错误

问题描述

如果我从 linux 命令行执行 mutt 命令,我会尝试在 linux 和 java 中使用 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();
        }
}

这里有什么问题?

标签: javalinuxruntimemutt

解决方案


您没有收到任何错误,因为您的系统上似乎有 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 中使用的 echo 不同,后者是内置的,行为有点不同......)

他们(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 命令,因为我不想发送邮件;)
  • 我通过以编程方式创建数组(“args”)来伪造 java 命令行。

  • 将您的 StreamGobbler 设为静态,以便将其保存为一个文件。

所有这些都不应该改变你的测试用例。有什么不同的是 rt.exec 调用执行 shell 而不是 /bin/echo

示例运行:

Execing echo "test" | grep -i "s" 
ExitValue: 0
OUTPUT>test

推荐阅读