如何验证外部进程是否异常终止?
How to verify if external process terminated abnormally?
以下代码片段运行外部进程:
ProcessBuilder pb = new ProcessBuilder("C:\Program Files\Java\jdk1.8.0_111\bin\java.exe",
"-cp",
"D:\nsd-rest\target\classes",
"com.dataart.ExternalProcess"); //java -cp D:\nsd-rest\target\classes my.package.ExternalProcess
Process process = pb.start();
System.out.println(process.waitFor(10, TimeUnit.SECONDS)); // prints true
外部流程如下所示:
public class ExternalProcess {
public static void main(String[] args) {
throw new RuntimeException();
}
}
尽管外部进程抛出异常,但第一个片段打印为真。
有没有办法验证进程是否异常完成?
来自Process#waitFor(长时间超时,
TimeUnit 单位)Javadoc
Returns:
true if the subprocess has exited and false if the waiting time elapsed before the subprocess has exited.
您可以使用 Process#exitValue() 检索退出值,并检查它是否为 != 0
Returns:
the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
以下代码片段运行外部进程:
ProcessBuilder pb = new ProcessBuilder("C:\Program Files\Java\jdk1.8.0_111\bin\java.exe",
"-cp",
"D:\nsd-rest\target\classes",
"com.dataart.ExternalProcess"); //java -cp D:\nsd-rest\target\classes my.package.ExternalProcess
Process process = pb.start();
System.out.println(process.waitFor(10, TimeUnit.SECONDS)); // prints true
外部流程如下所示:
public class ExternalProcess {
public static void main(String[] args) {
throw new RuntimeException();
}
}
尽管外部进程抛出异常,但第一个片段打印为真。
有没有办法验证进程是否异常完成?
来自Process#waitFor(长时间超时, TimeUnit 单位)Javadoc
Returns: true if the subprocess has exited and false if the waiting time elapsed before the subprocess has exited.
您可以使用 Process#exitValue() 检索退出值,并检查它是否为 != 0
Returns: the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.