File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Arka Sharma
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic