0

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?

2 Answers 2

1

You can use call or start to execute the java program

Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.