GeeCON Prague 2014*
The moose likes Java in General and the fly likes Issues with Runtime.exec Process.getInputStream and Process.waitFor(). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Issues with Runtime.exec Process.getInputStream and Process.waitFor()." Watch "Issues with Runtime.exec Process.getInputStream and Process.waitFor()." New topic
Author

Issues with Runtime.exec Process.getInputStream and Process.waitFor().

Arka Sharma
Ranch Hand

Joined: Jun 15, 2011
Posts: 103
Hi,
I have written a code to check whether HSQl, JBoss, Radius (AAA sever) and MySQL is running or not.The code is written in an inifinte while loop to continuously monitor.Now I tested that if this service(let's say hsql) is running then "/bin/bash -c ps -ef | grep 'hsql' | wc -l" will return 3 when this is passed as an argument in Runtime.exec() method.Now I have come to know following facts

1.The inputstream or errstream of the forked process must not be overflowed.Otherwise it 'll cause deadlock.

Keeping that in mind I have written this code



Output I'm expecting as follows

HSQL is running/not running 1
JBoss is running/not running 1
MySQL is running/not running 1
-------------------1----------------

HSQL is running/not running 2
JBoss is running/not running 2
MySQL is running/not running 2
-------------------2----------------

and so on...

Howevere I'm not getting like that.Some times HSQL's status message is not printed or sometimes JBoss status message is not printed.Even sometimes I got the output like
------------------1----------------------

after this status messages are printed.All I'm trying to say is that seems to be some race condition or some synchronization problem.

Thanks & Regards,
Arka




Charles Hargrave
Greenhorn

Joined: Apr 29, 2010
Posts: 11
Arka Sharma wrote:All I'm trying to say is that seems to be some race condition or some synchronization problem.


I wouldn't call it a problem exactly. Sometimes you want to execute a program and let it run independently while your Java program continues to run; it depends on what that program does.

About synchronous / asynchronous operation, yes, you are right. Quoting from the the JDK API's ProcessBuilder page:
There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.


If you need the program to run synchronously, it is possible. I've done this in the past but can't remember exactly how I did it; I'll have to get back to you on this. If you're not opposed to using API's for this, the Apache Commons Exec project might be useful to you, but its documentation assumes you know how Java executes other programs in detail. Executing other programs with Java can be tricky.

CNH
 
GeeCON Prague 2014
 
subject: Issues with Runtime.exec Process.getInputStream and Process.waitFor().