How to wait and close the command prompt in Java

I use the following code to execute the 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 until the batch file is executed I want to close the command prompt after executing the batch file How can I do this?

Solution

Using process Waitfor() lets your thread wait for the execution of the batch file to complete

java.lang.Runtime rt = java.lang.Runtime.getRuntime();
Process pr = rt.exec("MyBatch.bat");
pr.waitFor();

If you need to access the output and / or input of the console, you may also want to see using process builder instead of runtime getRuntime(). exec().

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>