How to give wait & close command line in java

I use 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 for the batch file to complete. I would like to close the command line after executing the batch file. How can i do this?

+3
source share
5 answers

Use Process.waitFor()to have your thread wait for the batch file to complete.

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

You can also look ProcessBuilderinstead Runtime.getRuntime().exec()if you need access to console output and / or input.

+8
source

- .waitFor() : pr.waitFor();

, , , .

+3

, Process.waitFor(). , ; , , , .

.

+1
0

InputStreamReader . bat.

EOF,

see example or full source code. Press here

0
source

Source: https://habr.com/ru/post/1703686/


All Articles