943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2363
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 6th, 2007
0

Please help me!!!

Expand Post »
Hi,

I am running an utility from java. The utility is a '.cmd' file. I have used the code below to run the utility.
Java Syntax (Toggle Plain Text)
  1. Runtime runTime = Runtime.getRuntime();
  2. try{
  3. runTime.exec("cmd /c start D:/Test/run.cmd");
  4. }
  5. catch(Exception e){

And it runs the utility successfully, the utility should be run 5 times and that too depends upon the status of the utility , I mean, if the utility runs successfully in the first iteration I have to run the second. Now my problem is : How should i know from the command prompt utility that it has been run successfully or failed in order to run the next iteration. I want to know , if there is any way to read from the Dos prompt window.

Frens this is very urgent task assigned to me.
Help would be greatly appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaygandhi559 is offline Offline
18 posts
since Jul 2007
Jul 6th, 2007
0

Re: Please help me!!!

Look at the Process class and its exitValue() method.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 6th, 2007
0

Re: Please help me!!!

Thanks for the reply , but the problem is ....I have to run the utility 5 times so i have kept it in the loop , before the utility is run for the first time ...it should read one properties file , and we need to change the attributes of properties file each time and run the utility . Now the problem is ....it is running 5 dos windows at a time , which should not, it should run only one instance of utility at a time, i'e only one dos window at a time . Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaygandhi559 is offline Offline
18 posts
since Jul 2007
Jul 6th, 2007
1

Re: Please help me!!!

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
Last edited by peter_budo; Jul 6th, 2007 at 9:33 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Jul 6th, 2007
0

Re: Please help me!!!

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:
java Syntax (Toggle Plain Text)
  1. Runtime runTime = Runtime.getRuntime();
  2. try{
  3. int exitCode = 0;
  4. for ( int runCount=0; runCount<5 && exitCode==0; runCount++ ) {
  5. // set whatever you need in properties file here
  6.  
  7. Process cmdProcess = runTime.exec("cmd /c start D:/Test/run.cmd");
  8. cmdProcess.waitFor();
  9. exitCode = cmdProcess.exitValue(); // will be 0 if successful
  10. }
  11. // if exitCode != 0 here, your process failed, so do whatever you need to do for that case
  12. } catch(Exception e){
  13.  
  14. }

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;
Last edited by Ezzaral; Jul 6th, 2007 at 1:21 pm. Reason: added note about cmd windows
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 7th, 2007
0

Re: Please help me!!!

Thanks for the help , that's what i was looking for. hope it should resolve the issue. Thank you very much
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaygandhi559 is offline Offline
18 posts
since Jul 2007
Jul 7th, 2007
1

Re: Please help me!!!

And you got it in the first post.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 31st, 2007
0

Re: Please help me!!!

Hi ...when i use the above code exactly ,
java Syntax (Toggle Plain Text)
  1. 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){ }Runtime runTime = Runtime.getRuntime();
  2. try{
  3. int exitCode = 0;
  4. for ( int runCount=0; runCount<5 && exitCode==0; runCount++ ) {
  5. // set whatever you need in properties file here
  6.  
  7. Process cmdProcess = runTime.exec("cmd /c start D:/Test/run.cmd");
  8. cmdProcess.waitFor();
  9. exitCode = cmdProcess.exitValue(); // will be 0 if successful
  10. }
  11. // if exitCode != 0 here, your process failed, so do whatever you need to do for that case
  12. } catch(Exception e){
  13.  
  14. }
it is starting the "run.cmd " command window...but the problem is , it is executing the statements below the code simultaneously......how to make the control stop there until my cmd window is finished.
Help will be greatly appreciated.
Last edited by vijaygandhi559; Jul 31st, 2007 at 2:17 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaygandhi559 is offline Offline
18 posts
since Jul 2007
Jul 31st, 2007
0

Re: Please help me!!!

.
Last edited by vijaygandhi559; Jul 31st, 2007 at 2:04 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaygandhi559 is offline Offline
18 posts
since Jul 2007
Jul 31st, 2007
0

Re: Please help me!!!

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.
Last edited by masijade; Jul 31st, 2007 at 2:54 am. Reason: typo
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Quick Question: Is J# the same thing as Java?
Next Thread in Java Forum Timeline: Problem with java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC