aspose file tools*
The moose likes Beginning Java and the fly likes another question about exec() to open a cmd window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another question about exec() to open a cmd window" Watch "another question about exec() to open a cmd window" New topic
Author

another question about exec() to open a cmd window

lei feng
Greenhorn

Joined: Aug 23, 2007
Posts: 26
for example:I use Runtime.getRuntime().exec("cmd /k dir"); the result is it run in the background. but i want to it show me the cmd window. how to do that? any help will be appreciated.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
I may be trying to do the same thing this week. It would be nice to get a response before then. This is another new thing for me. I have code to start a second application from the first, but haven't tried to start a command window for it.

I have little experience - but perhaps this will help. I think that the arguments might need to be in a String array.

Use a second argument if you need to set environmnet variables.
[ August 28, 2007: Message edited by: Roger F. Gay ]

Correlation does not prove causality.
lei feng
Greenhorn

Joined: Aug 23, 2007
Posts: 26
Originally posted by Roger F. Gay:
I may be trying to do the same thing this week. It would be nice to get a response before then. This is another new thing for me. I have code to start a second application from the first, but haven't tried to start a command window for it.

I have little experience - but perhaps this will help. I think that the arguments might need to be in a String array.

Use a second argument if you need to set environmnet variables.

[ August 28, 2007: Message edited by: Roger F. Gay ]


i don't understand what is the "environmnet variables"?
i've try it but i also can not get a cmd window.......so if i use exec to run a java program, i can not get the console information......so the point.....so i must get a cmd window by using runTime.exec("java -cp test doTest.test")
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Environment variables are things like path and classpath, that are set for the whole system. You can view or edit them through the Advanced tab in Systems on the Control Panel - which you bring up by clicking start and selecting Control Panel (in Windows). Control-Panel->System->Advanced-tab->Environment-variables-button

I don't understand how starting a Java program brings up a cmd window if you don't know how to bring up a cmd window from a Java program? How does doTest.test (.class?) bring up a cmd window?
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
If you want to use one string for the command (with arguments), rather than a command array, try ProcessBuilder.
[ August 28, 2007: Message edited by: Roger F. Gay ]
lei feng
Greenhorn

Joined: Aug 23, 2007
Posts: 26
Originally posted by Roger F. Gay:
Environment variables are things like path and classpath, that are set for the whole system. You can view or edit them through the Advanced tab in Systems on the Control Panel - which you bring up by clicking start and selecting Control Panel (in Windows). Control-Panel->System->Advanced-tab->Environment-variables-button

I don't understand how starting a Java program brings up a cmd window if you don't know how to bring up a cmd window from a Java program? How does doTest.test (.class?) bring up a cmd window?


that means i get two programs(p1,p2) to invoke each other. at first p1 run and when the condition is right p1 invoke the p2 before quit.but in this action, the p2 is running in background you can see the "cmd.exe,java.exe" in process explore but can not get another cmd window. and when some condtions is right p2 also invoke the p1 before quit. the situtation is the same as before. but i need the cmd window to see some System.out.print information. because our main program(whatever p1,p2) print the information to the cmd window. So i need cmd window......
lei feng
Greenhorn

Joined: Aug 23, 2007
Posts: 26
i found a method but i think this is not the best way to open the cmd window. that is using the .bat file. with .bat file in java program it can pop-up the cmd window...
lei feng
Greenhorn

Joined: Aug 23, 2007
Posts: 26
Hey guys i get the method to open another window(or explore) by using cmd "start"
String startCmd="cmd /k start D:\\";
p2=Runtime.getRuntime().exec(startCmd);
try it...it can also anyother window.....
but you'd better not write the "process.destory()"
[ August 28, 2007: Message edited by: lei feng ]
 
Consider Paul's rocket mass heater.
 
subject: another question about exec() to open a cmd window