Look at the Process class and its exitValue() method.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
If you searched forum before you posted, you would find that just yeasterday I linked another post with same quastion to my post where I asked same thing few months ago with full elaboration of the problem. Here is link http://www.daniweb.com/forums/thread73182.html , enjoy reading and next time search before you post, there is huge possibility somebody already made a topic with same/similar problem
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
As far as the cmd output and exit codes, see the thread that peter_budo posted a link to above. As for the loop, you can run it in the following loop based on exit code for success:
Runtime runTime = Runtime.getRuntime();
try{
int exitCode = 0;
for ( int runCount=0; runCount<5 && exitCode==0; runCount++ ) {
// set whatever you need in properties file here
Process cmdProcess = runTime.exec("cmd /c start D:/Test/run.cmd");
cmdProcess.waitFor();
exitCode = cmdProcess.exitValue(); // will be 0 if successful
}
// if exitCode != 0 here, your process failed, so do whatever you need to do for that case
} catch(Exception e){
}
Note: This will still start five command windows, you can't get around that, but each will close before the other opens since you are using waitFor() and checking the exit value;
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
And you got it in the first post.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
I'm not sure the code you posted there will even compile. You have a for block using a runCount variable inside of a try/catch block inside of a for block using a runCount varaiable (again) inside a try block with no catch block (at least not posted).
Clean that code up, first, then try to run it again, and if it still doesn't work, then ask again. And, once you do cmdProcess.waitFor(), any code below that point will not execute until the command has finished. Which, since you are not consuming the Streams of the process, might be never, if it produces enough output to fill the buffer.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
I gave you working solution on the beginning in linked post and you did not even considered to use when the other code did not work for you. What a shame....
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Here is a code as I used on the end to remove any images from Temp folder. Pased argument in for of string is "del C:" + File.separator + "Temp" + File.separator + "*.tif /F";
public class DeleteRutine
{
public void runDeleteRutine(String toExec)
{
try
{
// String storing the command to be executed
String cmdCommand = "C:/WINDOWS/system32/cmd.exe";
Process cmdProcess = Runtime.getRuntime().exec(cmdCommand);
BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(cmdProcess.getOutputStream()));
//Execute command
buffWriter.write( toExec, 0, toExec.length());
buffWriter.newLine();
buffWriter.flush();
buffWriter.close();
// Wait for the child to exit
cmdProcess.waitFor();
/*Just for checking purpose to see if CommandPropmt does it job
*and what is output of it*/
BufferedReader cmdOutStream = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
String temp = null;
while ((temp = cmdOutStream.readLine()) != null)
{
System.out.println(temp);
}
cmdOutStream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Clearly from thekashyap post on the line 14 you can see lChldProc.waitFor(); which state "Wait for the child to exit". Funny enough that "waitFor" is working perfectly.
>Please check the requirment before posting.
In the future please do not attack any other member or you may end up with no answers to your post...
Have nice day.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
You might try adding /wait to the start command you are using, since you are chaining process commands here:
Process p = r.exec("cmd /c start /wait run.cmd");
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847