Java waitFor()方法返回null,尽管进程已完成

Java waitFor()方法返回null,尽管进程已完成,java,Java,这是我的密码: private static model connectRemoteSession(String accountName,String password) throws IOException{ StringBuilder output = new StringBuilder(); StringBuilder error = new StringBuilder(); Runtime runtime

这是我的密码:

private static model connectRemoteSession(String accountName,String password) throws IOException{



            StringBuilder output = new StringBuilder();
            StringBuilder error = new StringBuilder();



            Runtime runtime = Runtime.getRuntime();


            String com = // some command

            proc = runtime.exec(com);
            Worker worker = new Worker(proc);
            reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            stderr = new BufferedReader(new InputStreamReader(
                    proc.getErrorStream()));
            String outLine;
            String errLine;
            worker.start();
            try {
                worker.join(300000);
                if (worker.exit != null){
                  //read the outout and error stream and take actions accordingly

                }

                else{
                    proc.destroy();
                    while ((outLine = reader.readLine()) != null)
                    {
                        CloudBackup.logger.info("online exchange output line    ="+outLine);
                        output.append(outLine);
                    }
                    while((errLine = stderr.readLine()) != null){
                        CloudBackup.logger.info("online exchange error line ="+errLine);
                        error.append(errLine);
                    }

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


        }catch(Exception e){
            CloudBackup.logger.severe(e.getMessage());
        }finally{
            reader.close();
            proc.getOutputStream().close();
            stderr.close();
        }

        return model;

    }

    class Worker extends Thread {
    private final Process process;
    Integer exit;
    Worker(Process process) {
        this.process = process;
    }
    public void run() {
        try { 
            exit = process.waitFor();
        } catch (InterruptedException ignore) {
            return;
        }
    }  
我面临的问题是,一台机器上的代码运行正常,但另一台机器上的worker.exit始终为null,尽管我将日志放在worker.exit==null中,并看到进程正在结束,但不知何故,process.waitFor没有捕获它


我知道process.waitFor()在Java8中可用,所以我检查了两个机器上的版本,它们的版本都是Java8。另外,没有其他线程正在运行。

试试这个,可能是缓存问题

 volatile Integer exit;

我在你的Worker.run中看到了一个危险的陷阱这不是
waitFor()
方法返回null的情况。这是一种方法在发生异常时不设置变量的情况。你的代码没有多大意义。在调用
waitFor()
exitStatus()。
之前,您应该阅读流程流。否则流程可能会阻止生成输出,并且永远不会退出。在异常情况下,我的变量将为null,这就是我稍后检查的内容。所以我认为这不是一个问题。在调用waitFor()之前,我正在读取进程流。在获得退出状态后,我只是检查流日志,这又有什么关系呢?垃圾。在调用
waitFor()之前,您没有读取进程流。
您正在(i)启动
waitFor()
线程,(ii)加入它,以及(iii)仅当退出状态不为null时才读取流。我已经解释了这有什么问题。在尝试读取其流之前,您还调用了
proc.destroy()
,这就没有什么意义了。试着解决它,而不是争论。NB
Process.waitFor()
自JDK1.0以来就一直存在。