【问题标题】:how to get error message when excuting java command?执行java命令时如何得到错误消息?
【发布时间】:2011-04-21 18:49:43
【问题描述】:

我在我的 java 代码中调用一个位于 jar 文件中某处的类(使用 java -classpath path/file.jar 类名)。

这很好用,但前提是命令格式正确。如果我犯了一个错误, getRuntime().exect(command) 就什么也不说。贝娄我有工作命令调用。当命令不起作用时,我想收到 error 消息。如果我在 cmd (windows) 中犯了一个错误,我会得到一个正确的错误,我可以修复它。但不在我的 java 应用程序中。

我留下了一个“if(input.ready())”,因为如果我不这样做,程序会在命令行不正确时冻结。执行 'input.readLine()' 时会发生这种情况。

        // Execute a command with an argument that contains a space
        String[] genKOSCommand = new String[] {
                "java",
                "-classpath",
                Config.XDSI_TEST_KIT_HOME + "/xdsitest/lib/xdsitest.jar;"
                        + Config.XDSI_TEST_KIT_HOME + "/xdsitest/classes",
                "ca.etsmtl.ihe.xdsitest.docsource.SimplePublisher", "-k",
                "C:/Softmedical/Viewer_Test/xdsi-testkit-2.0.4/xdsihome/usr/data/image14.dcm" };

        Process child = Runtime.getRuntime().exec(genKOSCommand);

        BufferedReader input = new BufferedReader(new InputStreamReader(
                child.getInputStream()), 13107200);

        String line = null;

        if (input.ready()) {
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }

            try {
                child.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

对如何从执行的命令中获取错误有任何建议吗?

谢谢

【问题讨论】:

    标签: java


    【解决方案1】:

    这里有一段更完整的代码,用于打印通过 Process/Runtime 运行一些 command 时收到的错误:

    final String command = "/bin/bash -c cat foo.txt | some.app";
    Process p;
        try {
            p = Runtime.getRuntime().exec(command);
        } catch (final IOException e) {
            e.printStackTrace();
        }
    
        //Wait to get exit value
        try {
            p.waitFor();
            final int exitValue = p.waitFor();
            if (exitValue == 0)
                System.out.println("Successfully executed the command: " + command);
            else {
                System.out.println("Failed to execute the following command: " + command + " due to the following error(s):");
                try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
                    String line;
                    if ((line = b.readLine()) != null)
                        System.out.println(line);
                } catch (final IOException e) {
                    e.printStackTrace();
                }                
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      【解决方案2】:

      通过使用 getErrorStream:

      BufferedReader errinput = new BufferedReader(new InputStreamReader(
                      child.getErrorStream()));
      

      在处理来自不同流的输入时,最好在不同的线程中执行(因为那些调用(readLine 等)是阻塞调用。

      【讨论】:

      • 看来他还是想等待进程终止。
      • 我同意,但您不能在同一个线程中并行读取两个流。
      • @glowcoder - 因为你不知道在哪个流中你会得到下一行。
      • @MByD 所以如果我启动线程来读取流,这只会避免阻塞我的主进程。但是 readLine 还是会阻塞线程,不是吗?
      • 是的,但这将是一个单独的线程。我看到了一个很好的例子,我仍在寻找它。当我找到它时告诉你。
      【解决方案3】:

      Process.getErrorStream不是你想要的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        相关资源
        最近更新 更多