java.lang.Process.waitFor()方法将导致当前的线程等待,如果必要的话,直到由该Process对象表示的进程已经终止。此方法将立即返回,如果子进程已经终止。如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出。

public class ProcessDemo {

public static void main(String[] args) {
try {
// create a new process
System.out.println("Creating Process...");
Process p = Runtime.getRuntime().exec("notepad.exe");

// cause this process to stop until process p is terminated
p.waitFor();

// when you manually close notepad.exe program will continue here
System.out.println("Waiting over.");

} catch (Exception ex) {
ex.printStackTrace();
}

}
}