views:

1159

answers:

3

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.

+6  A: 

Don't use start, that opens another window

Replace:

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

with:

Runtime.exec("cmd /c ....")
Pyrolistical
You are so right. I didn't even notice that "start" in the command string, even when I was formatting it.
Michael Myers
I didn't know what start did until now ;).
Amara
@Amara: in a cmd window, type START /? for help on that command.
Mr. Shiny and New
+2  A: 

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).

Mr. Shiny and New
I'm trying to run python scripts and batch files. Is that possible to run directly in Java and how would I start? (I think I need to pose this as a question? :) )
Amara
@Amara: Python's script interpreter is a program, as is cmd.exe. Both can be used with command-line arguments to execute their relative scripts (.py or .bat files). Also there is Jython for executing python right in the VM, if you are interested.
Mr. Shiny and New
A: 

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. :-)

Paulo Guedes
Minimizing the window doesn't really get rid of it.
Mr. Shiny and New
@Shiny Remind the meaning of "suggestion".
Paulo Guedes
The option of putting in a batch file might work better. I have tried the cmd /c with out start and my commands won't run. So I'll try the batch file route. Thanks everyone! I learned a lot :)
Amara