In java determine if a process created using Runtime environment has finished execution?

后端 未结 4 1235
一整个雨季
一整个雨季 2021-01-13 11:08
Runtime.getRuntime.exex(\"abc.exe -parameters\");

using .waitFor() does not help to determine the completion of process.

4条回答
  •  自闭症患者
    2021-01-13 11:13

    Looks like JDK8 introduces Process.isAlive(). Surprised it took so long...

    In the meantime, the best option seems to be to poll Process.exitValue(), wrapped in a try-catch:

    // somewhere previous...
    String[] cmd = { "abc.exe", "-p1", "-p2" };
    Process process = Runtime.getRuntime.exec(cmd);
    
    // call this method repeatedly until it returns true
    private boolean processIsTerminated () {
        try {
            process.exitValue();
        } catch (IllegalThreadStateException itse) {
            return false;
        }
        return true;
    }
    

    Alternately, a similar method could return the exit value if the process had terminated, or some other specified value if not.

提交回复
热议问题