Reputation: 11
I have a batch file called 'StartUpdate.bat' which contains something like this:
set CLASSPATH="myclasspath"
java -cp %CLASSPATH% UpdateProgram
runMyApp.bat
If I run 'StartUpdate.bat' directly from command line, it executes UpdateProgram and then runMyApp.bat immediately after. This is the intention.
However, if I call 'StartUpdate.bat' from another Java program, it terminates immediately after completing UpdateProgram. 'StartUpdate.bat' is called from this other Java program using
Runtime.getRuntime().exec(path + "StartUpdate.bat");
StartUpdate.bat is executed just fine, as is UpdateProgram inside it, but nothing else following UpdateProgram.
Why does it behave this way? What should I do so that it executes the remainder of the batch file?
Upvotes: 0
Views: 62
Reputation: 109593
Explicitly use a user Thread with setDaemon(false)
. It seems that there was the problem.
As long as there is a user (non-daemon) thread, the JVM will keep the application alive. Daemon threads are closed when no user threads exist anymore.
As daemon threads are typically used for such "server" like purposes, an often misconception.
For the rest ProcessBuilder would be a more robust class for this task.
ProcessBuilder pb = new ProcessBuilder("dir");
Process process = pb.start();
int returnCode = process.waitFor();
Upvotes: 0