I am using the following code to execute a batch file:
java.lang.Runtime rt = java.lang.Runtime.getRuntime();
Process pr = rt.exec("MyBatch.bat");
My batch file takes some time to execute. I want my servlet process to wait till the batch file execution completes. I would like to close the command prompt after executing the batch file. How can I do this?
Use Process.waitFor()
to have your thread wait for the completion of the batch file's execution.
java.lang.Runtime rt = java.lang.Runtime.getRuntime();
Process pr = rt.exec("MyBatch.bat");
pr.waitFor();
You may also want to look at using ProcessBuilder
instead of Runtime.getRuntime().exec()
if you need access to the console's output and/or input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With