3

I have opened a commnad prompt using java program and another command prompt manually. Now my requirement is that i need to close the command prompt that i have opened by java program using the program.

I tried to close this by giving rt.exec("taskkill /IM cmd.exe");

But the problem is that the command prompt that i have opened manually is aslo closed which i dont want.

Help needed. Thanks in advance

3 Answers3

1

It's simple -

Step 1 - Create a batch file (say closeCMD.bat)

Step 2 - write exit in closeCMD.bat

Step 3 - call above batch file in your java code as below -

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

That's it !! Cheers!!

star95
  • 111
  • 3
  • 16
  • I start selendroid standalone server programmatically and follow your way to close cmd but it not work. Is there any way else to close cmd in this case? Tks! – Huy Hóm Hỉnh Jan 12 '16 at 04:51
  • Do you have more than one instance of cmd prompt open? In that case, it might not work. Another approach to close cmd is to kill the process of cmd using Runtime object. Here's the link -http://stackoverflow.com/questions/6356340/killing-a-process-using-java – star95 Jan 15 '16 at 15:27
1

if you're using Runtime.getRuntime().exec(), it returns a Process object. You should hold on to that and call Process.destroy() when you're done.

John Ellinwood
  • 14,291
  • 7
  • 38
  • 48
0

you can try to program "exit" into the command prompt after you are done with your tasks in command prompt

ken
  • 193
  • 1
  • 3
  • 8