views:

2877

answers:

4

Hi

I know how to start the batch file using java code. When i run the batch file command prompt is opened. To close the command prompt i am using the taskill /im cmd.exe. but the problem is that the command prompt that is used to start the jboss is also closed. i want to kill the cmd with a particular process id. How do i get the process id of a particular cmd promt and kill that using java

+2  A: 

Run the batch file with cmd.exe /c job.bat. The /c switch carries out the command and then terminates the command interpreter.

Zach Scrivena
A: 

can't you add exit to your batch file to exit it's own command prompt. Uisng taskill seems overkill for just closing one command prompt, don't you think?

PS: I've never worked on batch files just the command prompt so I'm assuming it accepts the same commands.

hhafez
ur idea is good.but the problem is that i want to close it after 15 minutes. if i give exit in the batch file then it will close immediately.
zach's answer doesn't wait for 15 minutes either!
Chandan .
+1  A: 

If you start the batch file with Runtime.exec(), it returns you a Process object. Calling the destroy() method will kill that process.

Maurice Perry
it will kill the process but doenot close the command prompt
@abc: You must combine this tip with the "cmd /c".
Aaron Digulla
A: 

Hi folks... I too had the same problem. I first used as

Runtime.getRuntime().exec("cmd.exe start /c test.bat");

Then I tried as below. It works fine.

Runtime.getRuntime().exec("cmd.exe /c start test.bat");

Try this... Have fun...

abi