Scenario was i started the command prompt manually. Then i need to close the Command prompt automatically using Java. How i can close the command prompt using java. Any one can help me
-
Possibly duplicate of http://stackoverflow.com/questions/618528/close-a-particular-command-prompt – ethree Jul 17 '13 at 07:04
-
1@sureshatta How is it duplicate? IN the question, the command prompt is opened manually,not thru java. – rahulserver Jul 17 '13 at 07:07
5 Answers
Use the following code:
class CmdKill
{
public static void main(String args[])
{
try {
Runtime.getRuntime().exec("taskkill /f /im cmd.exe") ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Surely would help!!
Edit:
To kill a particular process, if u have the PID(from task manager.) you can use taskkill with /pid option.Refer for options here.But anyways you wont have the pid without looking into the task manager.The only solution seems as suggested by this SO answer that you start the process and get hold of the reference.

- 1
- 1

- 10,411
- 24
- 90
- 164
-
2Cool solution. I did not know about `/im`. However I am afraid it will kill *all* `cmd.exe` that are currently running instead of only one that originated this java process – AlexR Jul 17 '13 at 07:18
-
Above code is not closing the command prompt, if already opened – Saravanakumar Marudhachalam Jul 17 '13 at 07:19
-
@SaravanakumarMarudhachalam how come! it works for me!and you are right.It will kill all cmd.exe opened – rahulserver Jul 17 '13 at 07:19
-
it worked on my pc, but when I installed it to the the server where everybody can use the project, It didn't close the cmd window after I exit the called .bat file: Process p = Runtime.getRuntime().exec("cmd /c start CC.bat"); p.waitFor(); Runtime.getRuntime().exec("taskkill /f /im cmd.exe") ; – Chris Sim Jul 15 '15 at 07:10
Since command prompt is a separate platform specific program you do not have any cross platform solution for this. You can however execute command exit
via Runtime.getRuntime().exec()
or process builder. It will hopefully work.
Fortunately this command has the same syntax on both windows and unix.
The question is whether you want to close command prompt while your program is still running or when program is terminated.
I think that in both cases better solution is to run your java program using platform specific script (e.g. bat file for windows and shell script for unix) that is responsible on closing the prompt.
If you want to run your program (that for example creates window) and close the prompt while the program is still running you can use javaw
instead of java
on windows.

- 114,158
- 16
- 130
- 208
-
Hi Alex, i want to close the Command prompt while program is still running – Saravanakumar Marudhachalam Jul 17 '13 at 07:20
For this scenario you can execute command like
Runtime.getRuntime().exec("command.exe /C" + "Your command");
This will open command prompt executes command and then close it.
Hope this will work

- 1,409
- 1
- 15
- 26
-
It is not wanted that the command prompt be opened by java!Read question carefully – rahulserver Jul 17 '13 at 07:18
-
2It is!!!!!,But if he will execute command using this way then he dont need to close it buddy. I'm not here for points!!! like you :p – Jaykishan Jul 17 '13 at 07:21
Here is how i did it mate
1) create a batch file with the following contain java -Xms512M -Xmx512M -jar yourFileName.jar -o true EXIT
2) withing ur exit input stream add System.exit(1);

- 1