Home arrow static arrow Java Programming [Archive] - Problem with Runtime.exec() ... bug?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem with Runtime.exec() ... bug?
This topic has 3 replies on 1 page.

Posts:403
Registered: 9/4/03
Problem with Runtime.exec() ... bug?  
Aug 4, 2004 1:28 PM



 
I am using the following code to try and execute an .exe.

String cmdString = ("CMD /C \"C:\\temp\\psexec \\\\AUNTDB01 -u peoplesoft\\d_user -p Beagle11 -d -c C:\\temp\\InstallJarwin.bat\"");try{	//The .exec syntax: (Command string, env, run directory)    System.out.println("Executing: " + cmdString);	Process p = Runtime.getRuntime().exec(cmdString);	// Get the std output from the process (Yes, they are labelled backwards).	// So the input stream is used to grab the output from the process.	// And the output stream is used to pipe data into the process.	InputStream is = p.getInputStream();	// Get the std in to the process.	OutputStream os = p.getOutputStream();	// Get the std err from the process.	InputStream es = p.getErrorStream();	// Create readers for those streams.	BufferedReader reader = new BufferedReader(new InputStreamReader(is));	BufferedReader errReader = new BufferedReader(new InputStreamReader(es));	String line;					// Read STDOUT into a buffer.	while((line = reader.readLine()) != null){		// Do something with data here if you wish.	    System.out.println( line );	}	// If no STDOUT check STDERR.	while((line = errReader.readLine()) != null){		// Do something with data here if you wish.	    System.out.println( line );	}				// Wait for the process to end.	p.waitFor(); 	Thread.sleep(30000);}catch( IOException ex ){	String strPrint = "Caught IOException trying to launch task. Please ensure your launch string is correct.";	System.out.println( strPrint );			}catch( InterruptedException ex ){	String strPrint = "Caught InterruptedException: " + ex.getMessage().toString();		System.out.println( strPrint );}	


The problem is that the process hangs and does not return anything. Also, nothing is printed out in the read loop. I can see psexec.exe running in the process list but it will not go away.

If I run this command on the command line, it works as expected. I use this:
CMD /C "C:\temp\psexec
AUNTDB01 -u peoplesoft\d_user -p Beagle11 -d -c C:\temp\InstallJarwin.bat"

Any ideas?
 

Posts:2,206
Registered: 8/15/02
Re: Problem with Runtime.exec() ... bug?  
Aug 4, 2004 1:36 PM (reply 1 of 3)



 
1. remove the Thread.sleep()
2. Carefullt read [url=http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]this[/url]
 

Posts:403
Registered: 9/4/03
Re: Problem with Runtime.exec() ... bug?  
Aug 4, 2004 1:58 PM (reply 2 of 3)



 
Yes, I have read that.

The problem is fixed. Thread.sleep() was not the issue as it is only sleeping after the process has exited. All reading of stdout and stderr is completed by the time the sleep() method is ever called.

The issue was with the order in which I was reading stout/stderr.

Apparently, psexec is writing all its output to stderr for some reason. When I switched the order of the reads so that I checks stderr first, it works fine.

I will look at reading both stdout and stderr concurrently with separate threads in the very near future. But for now it works for what I need it to.

Thanks.
 

Posts:2,206
Registered: 8/15/02
Re: Problem with Runtime.exec() ... bug?  
Aug 4, 2004 2:02 PM (reply 3 of 3)



 

I will look at reading both stdout and stderr
concurrently with separate threads in the very near
future. But for now it works for what I need it to.

Thats the solution!
 
This topic has 3 replies on 1 page.