| | |
Please help me!!!
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 18
Reputation:
Solved Threads: 0
Hi,
I am running an utility from java. The utility is a '.cmd' file. I have used the code below to run the utility.
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.
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)
Runtime runTime = Runtime.getRuntime(); try{ runTime.exec("cmd /c start D:/Test/run.cmd"); } 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.
Look at the Process class and its exitValue() method.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jul 2007
Posts: 18
Reputation:
Solved Threads: 0
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.
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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:
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;
java Syntax (Toggle Plain Text)
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;
Last edited by Ezzaral; Jul 6th, 2007 at 1:21 pm. Reason: added note about cmd windows
And you got it in the first post.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jul 2007
Posts: 18
Reputation:
Solved Threads: 0
Hi ...when i use the above code exactly ,
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.
java Syntax (Toggle Plain Text)
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(); 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){ }
Help will be greatly appreciated.
Last edited by vijaygandhi559; Jul 31st, 2007 at 2:17 am.
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.
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
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Other Threads in the Java Forum
- Previous Thread: Quick Question: Is J# the same thing as Java?
- Next Thread: Problem with java
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






