I am trying to write a small tool that restarts a certain number of jar’s if they have been terminated.
private static void checkAndRestartProcesses() {
for ( int i=0; i<processes.size(); i++ ) {
try {
processes.get(i).exitValue();
processes.remove(i);
} catch (IllegalThreadStateException ex) {
}
}
try {
for ( int i=processes.size(); i<Config.NR_PROCESSES; i++ ) {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c start cmd /c java -jar bla.jar");
Process p = pb.start();
processes.add(p);
}
} catch (IOException ex) {
}
}
The problem is that processes started with “cmd start” exit right after they execute the sub-process (and thus, there is no IllegalThreadStateException).
How can I keep track of the subprocess (whether they are terminated or not)?
Or alternatively: How can I start a sub-process in a separate cmd without using “cmd start”.
I need the started routines to be in a new cmd.
Thanks a lot in advance.
You need to use the /wait flag on start