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?
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
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(); ...
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.)