Eddie Martinez
Eddie Martinez

Reputation: 13910

runtime exec commands do not execute successively

I am trying to run various commands consecutively with runtime exec. I make an instance of the getRuntime method and using the same instance I call different commands successively but they all execute at the same time. If runtime exec is not blocking, what is a good way to execute the second command when the first one is done ?

Runtime runTime = Runtime.getRuntime();

runTime.exec(new String[]{"sh", "-c", "some command"});
runTime.exec(new String[]{"other command"});
runTime.exec(new String[]{"sh","-c","final command"});

Upvotes: 0

Views: 157

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I would use waitFor.

runTime.exec(new String[]{"sh", "-c", "some command"}).waitFor();
runTime.exec(new String[]{"other command"}).waitFor();
runTime.exec(new String[]{"sh","-c","final command"}).waitFor();

Upvotes: 2

Andrew
Andrew

Reputation: 8703

You need to use waitFor():

Process p = runTime.exec(...)
int exitValue = p.waitFor()
System.out.println("Exit value:  " + exitValue;

wash rinse repeat

Upvotes: 1

Related Questions