Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to give wait & close the command prompt in java

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?

like image 503
user48094 Avatar asked Sep 19 '25 17:09

user48094


1 Answers

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.

like image 130
vladr Avatar answered Sep 21 '25 06:09

vladr