• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Runtime.getRuntime().exec

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good to me. The only thing I wondering (and it's probably inconsequential) is why you're explicitly closing the IS/OS/ES.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic