Hi,

I am running an utility from java. The utility is a '.cmd' file. I have used the code below to run the utility.

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.

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

commented: Seriously donno when will they learn to use search.. ! +3

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;

Thanks for the help , that's what i was looking for. hope it should resolve the issue. Thank you very much

And you got it in the first post.

commented: too right! +11

Hi ...when i use the above code exactly ,

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){
    
}

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.

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.

Hi, this is the simple code , i am trying .....

public class Testing {

public static void main(String[] args) {

Runtime r = Runtime.getRuntime();
System.out.println("Calling Run "); 
try{
Process p = r.exec("cmd /c start run.cmd");
System.out.println("hi");
p.waitFor();
System.out.println("Process terminated !!!");
}//try
catch(Exception e)
{
System.out.println("Exception occurred :"+e);
}

}//main
}//class

The command window starts , but the problem is the line "Process Terminated " displays before command window closes. Is it operating system dependant , for your info. I am using Windows XP.

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....

What working version? I have gone through the link on that day itself. May be you didn't get my requirement clearly . My problem is not running the utility, but making my code to wait until the utility ends. process.waitFor() or process.exitValue() doesn't work here. Please check the requirment before posting.

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.

Thank you very much for helping me. My intention was not to attack on any member. process.waitFor() , only works when we are trying to read the InputStream from the command window. If only process.waitFor() is used , it's not working . I have tried it with reading the InputStream and it works perfect. But here i don't need the InputStream and process.waitFor() is not making the code to wait, if I don't use the InputStream. I 've checked it. Believe me.
In your previous post
>What a shame ...
How can you assume that i have'nt tried your code. I really thank you very much for helping me.
You too have a nice day.

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");

Thank you, I have also tried removing the "start" from the code and it works perfect ...it waits for the utility to complete, but the only problem in removing "start" is it doesn't display the utility command window...the process runs in the background...Let me try using "wait" . I will let you know with the result.
Thank you ...

Thank you very much...it is solved now .it is waiting now...however, the utility not getting closed by itself and i have written "exit " in the "run.cmd "file.But , by using "/wait" in the command line string ,my problem has been really solved. Than you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.