Tutorial :How to close command windows with Java



Question:

Each time I use Runtime.exec("cmd /c start....") I am opening a cmd window. I would like to create a more seamless application by closing each previous cmd window. How can I do this?

If anyone knows of a better way to run a series of commands in the same cmd window rather than opening new ones each time I execute a command, please let me know.


Solution:1

You cannot control the opened windows from Java. But I suggest two solutions for you:

First:

Runtime.exec("cmd /c start /MIN ...")  

The /MIN parameter will prevent windows from appearing.

Second: put all the applications you must call inside the same batch file and call it once. :-)


Solution:2

Don't use start, that opens another window

Replace:

Runtime.exec("cmd /c start....")  

with:

Runtime.exec("cmd /c ....")  


Solution:3

Why do you need cmd windows?

I'd run the command directly in Java and capture the output and display it in a local window (if it's needed at all).

Just make sure you consume all the output from the program or it might stall. Also remember to close the stdout, stderr, and stdin streams or some file handles might leak (well, that's true on some JDKs on some Unix OSes... Windows, who knows).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »