我刚刚通过 exec() 调用创建了一个进程,现在我正在使用它的 .waitFor() 方法。我需要捕获一个 InterruptedException,但我不确定我应该在 catch 代码块中放置什么。我想收到退出代码,但如果当前线程被中断,我不会收到。如果线程被中断,我应该怎么做才能让退出代码退出进程?
例子:
import java.io.IOException;
public class Exectest {
public static void main(String args[]){
int exitval;
try {
Process p = Runtime.getRuntime().exec("ls -la ~/");
exitval = p.waitFor();
System.out.println(exitval);
} catch (IOException e) {
//Call failed, notify user.
} catch (InterruptedException e) {
//waitFor() didn't complete. I still want to get the exit val.
e.printStackTrace();
}
}
}