Java – command to close the batch file in java


Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

I am running the batch file by means of the above lines in java.
The command prompt will be opened.
I need to close the command prompt using java code


Best Solution

rt.exec("taskkill /IM cmd.exe");

or when you run your batch file

rt.exec("cmd /C batchfile.bat");

The second option is better in your situation. Less code, the command prompt exits once the batch is complete. The first option just kills an instance of cmd.exe open (which you may have multiple ones open).

to make sure it has finished running: