File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Runtime.getRuntime().exec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.getRuntime().exec" Watch "Runtime.getRuntime().exec" New topic
Author

Runtime.getRuntime().exec

Jeff Long
Greenhorn

Joined: Feb 27, 2002
Posts: 5
I'm writing a gui based installer program in java that can be used on linux. All that the gui is gonna do is install some rpms. I plan to do it
using Runtime.getRuntime().exec("command"). From what I understand, this creates a process and runs the command in that process.
I have a series of commands to execute and I want to make sure that they are run in the order i give, or in otherwords in the following code

I want to make sure command2 is executed only after command1 has executed completely.
After googling I found out that I can do the following

I just want to make sure what I'm doing is right, or is there any hidden problem?
Also is there a better way to write to gui-based setup program for linux environment?
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Looks good to me. The only thing I wondering (and it's probably inconsequential) is why you're explicitly closing the IS/OS/ES.


There's nothing more dangerous than a resourceful idiot. ---Dilbert
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Jeff,
if u r on Unix then can u use "script" to have this if else conditions based upon pervious process failure and achieve what u want?
if u r on Windows then i guess u must be able to do "batch" file thing and do the same (i'm not really sure as i've never done this on Windows..u know getting the last process's return code etc..but for Unix i'm sure u can do this)
then u can run that script (or batch file) which will do things for you.
i think this would be easier than anything in Java (u will use just one Process in java).
regards,
maulin
Fred Close
Ranch Hand

Joined: Mar 13, 2001
Posts: 42

isn't the waitFor method what you are looking for ?
Process p = Runtime.getRuntime().exec("command1");
p.waitFor(); //p2 will not run until p1 is finished
Process p2 = Runtime.getRuntime().exec("command2");
p2.waitFor();
...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Jeff already is calling waitFor(). However there is a problem - closing all the streams before the process has completed may quite possibly cause problems. I'd put these after the waitFor() instead. Moreover, I'd almost certainly want to read the ErrorStream first in case there's any problem. Possible also the InputStream in case the process has any output you need to know. If the process doesn't require any additional input, you can safely ignore the OutputStream. (Remember that input for the Process is output for your program, and vice versa.)


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.getRuntime().exec
 
Similar Threads
On Runtime and Process
Dos Shell in JAVA
Cannot see exec()'ed app
To close an application that has been opened previously
Executing Unix command from java : problem