Reputation: 7827
I need to start a batch using java and after a few seconds i need to stop the running batch file.
here is my code
try {
Process process=Runtime.getRuntime().exec("cmd /c start D:/test.bat");
Thread.sleep(20*1000);
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
my code failed to stop the batch file.
Upvotes: 1
Views: 4800
Reputation: 617
If you run a java
or javaw
on your .bat file, you need to terminate your batch file first:
Runtime.getRuntime().exec("taskkill /f /im jqs.exe") ;
Runtime.getRuntime().exec("taskkill /f /im javaw.exe") ;
Runtime.getRuntime().exec("taskkill /f /im java.exe") ;
And stop cmd(if needed)
Runtime.getRuntime().exec("taskkill /f /im cmd.exe") ;
Hope this help!
Upvotes: 0
Reputation: 1
If the batch is running in cmd.exe using Java, run this
Process prClose = Runtime.getRuntime().exec("taskkill /im cmd.exe");
Upvotes: 0
Reputation: 340708
It isn't that simple since you have to kill an independent operating system process you have just started. Looks like How to find and kill running Win-Processes from within Java? might help you.
In short: you have to find the PID of a batch script (actually, of a cmd
process running your batch script) and kill it using taskkill
.
Upvotes: 2