java运行时getruntime exec timeout返回exit always NULL和always TimeoutException?

java运行时getruntime exec timeout返回exit always NULL和always TimeoutException?,java,Java,指 但我总是得到worker.exit值NULL,所以它总是抛出超时异常。下面是我的代码 public class MyClass { private static class Worker extends Thread { private final Process process; private Integer exit; private Worker(Process process) { this.p

但我总是得到worker.exit值NULL,所以它总是抛出超时异常。下面是我的代码

public class MyClass {


    private static class Worker extends Thread {
        private final Process process;
        private Integer exit;

        private Worker(Process process) {
            this.process = process;
        }

        public void run() {
            try {
                exit = process.waitFor();
            } catch (InterruptedException ignore) {
                return;
            }
        }
    }


    public String run(String command, long replyTimeout) throws Exception {

        StringBuffer output = new StringBuffer();
        Process p;
        p = Runtime.getRuntime().exec(command);

    BufferedReader errReader = new BufferedReader(new InputStreamReader(
            p.getErrorStream()));

    BufferedReader inputReader = new BufferedReader(new InputStreamReader(
            p.getInputStream()));

        Worker worker = new Worker(p);
        worker.start();

        try {
            worker.join(replyTimeout);              


            if (worker.exit != null) {
                if (worker.exit > 0) {

                    String line = "";
                    while ((line = errReader.readLine()) != null) {
                        output.append(line + "\n");
                    }
                    System.out.println(output.toString());
                    System.out.println(worker.exit);
                    throw new Exception(output.toString());
                } else {

                    String line = "";
                    while ((line = inputReader.readLine()) != null) {
                        output.append(line + "\n");
                    }
                    System.out.println(output.toString());
                    System.out.println(worker.exit);
                    return output.toString();
                }
            } else {
                throw new TimeoutException();
            }

        } catch (InterruptedException ex) {
            worker.interrupt();
            Thread.currentThread().interrupt();
            throw ex;
        } finally {
            p.destroy();
        }

    }



}

你做错了。在调用waitFor()之前,您必须在stdout和stderr上使用进程的所有输出。否则,进程可能会阻止尝试写入自己的输出


另一方面,NullPointerException只是由于一些琐碎的编码错误造成的,您希望能够自己解决这些错误。至少我是这么想的。

你做错了。在调用waitFor()之前,您必须在stdout和stderr上使用进程的所有输出。否则,进程可能会阻止尝试写入自己的输出


另一方面,NullPointerException只是由于一些琐碎的编码错误造成的,您希望能够自己解决这些错误。至少我是这么想的。

这里就是这么说的。你知道为什么退出值总是空的吗?不知道,但你知道。您有堆栈跟踪,您有抛出它的行号,您有带有该行号的代码行,您有在该行上被取消引用的变量,您有它以前的历史记录。你不需要任何人的帮助。另一方面,我们没有这些。这里就是这样提到的。你知道为什么退出值总是空的吗?不,但你知道。您有堆栈跟踪,您有抛出它的行号,您有带有该行号的代码行,您有在该行上被取消引用的变量,您有它以前的历史记录。你不需要任何人的帮助。另一方面,我们没有这些。