473,574 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about Process.waitFor

dmjpro
2,476 Top Contributor
Have a look at my code snippet.

Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("my command");
  2. l_p.waitFor();  //here the program hangs out
  3.  
Then i go for a site and found it's solution ..
Simply i access the input as well error stream before calling wait for ...
here my updated one goes ...

Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("my command");
  2. InputStream output = l_p.getInputStream();
  3. InputStream error = l_p.getErrorStream();
  4. if(output!=null){
  5.   //reading the output
  6. }
  7. if(error!=null){
  8.   //reading error
  9. }
  10. l_p.waitFor();
  11.  
How it solves the problem ....?
Please explain.
Aug 12 '08 #1
17 11648
JosAH
11,448 Recognized Expert MVP
Read the process' output and error stream in other threads because if your main
thread syncs with the process' thread more output can follow from that process.
What you wrote doesn't work.

kind regards,

Jos
Aug 12 '08 #2
Nepomuk
3,112 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("my command");
  2. l_p.waitFor();  //here the program hangs out
  3.  
Little correction in your english: The Program probably doesn't "hang out" in any place. It hangs.
Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("my command");
  2. InputStream output = l_p.getInputStream();
  3. InputStream error = l_p.getErrorStream();
  4. if(output!=null){
  5.   //reading the output
  6. }
  7. if(error!=null){
  8.   //reading error
  9. }
  10. l_p.waitFor();
  11.  
How it solves the problem ....?
Please explain.
As far as I remember, the program has a certain output which the system wants you to read. This output is in the buffer and the program won't continue, until the buffer has been emptied.

I'm sure, someone like Jos or r0 can explain in more detail, but that should give the general idea. Always make sure you empty the buffer when running a system call.

Greetings,
Nepomuk
Aug 12 '08 #3
dmjpro
2,476 Top Contributor
Read the process' output and error stream in other threads because if your main
thread syncs with the process' thread more output can follow from that process.
What you wrote doesn't work.

kind regards,

Jos
Thanks for your help Josh.
So you mean to say that the reading of error stream or output stream ...executes in separate thread, And the subprocess runs in a separate thread.
"because if your main
thread syncs with the process' thread more output can follow from that process" Please could give me more details here ?
And what code is not running, i am not getting you.
Aug 12 '08 #4
JosAH
11,448 Recognized Expert MVP
Thanks for your help Josh.
So you mean to say that the reading of error stream or output stream ...executes in separate thread, And the subprocess runs in a separate thread.
"because if your main
thread syncs with the process' thread more output can follow from that process" Please could give me more details here ?
And what code is not running, i am not getting you.
As long as that process exists none of the streams hit en end-of-file condition;
that means that more output (or input) can follow after your main thread has
joined with the process thead. Your code would fail.

kind regards,

Jos
Aug 12 '08 #5
dmjpro
2,476 Top Contributor
As long as that process exists none of the streams hit en end-of-file condition;
that means that more output (or input) can follow after your main thread has
joined with the process thead. Your code would fail.

kind regards,

Jos
Yeah now i got ... Thanks Josh.
Now why my code wouldn't run?
Aug 12 '08 #6
JosAH
11,448 Recognized Expert MVP
Yeah now i got ... Thanks Josh.
Now why my code wouldn't run?
'fail' as in 'not do its job properly'.

kind regards,

Jos
Aug 12 '08 #7
dmjpro
2,476 Top Contributor
Josh "hit en end-of-file condition" what does that mean?
And i tried out a code ..but not had fruitful result.
I am using Windows ..now i came to know that runtime.exec behaves OS specific .

Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("javac");
  2.         InputStream output = l_p.getInputStream();
  3.         InputStream error = l_p.getErrorStream();
  4.         BufferedReader _output_ = new BufferedReader(new InputStreamReader(output));
  5.         BufferedReader _error_ = new BufferedReader(new InputStreamReader(error));
  6.         String line = null;
  7.         while((line=_output_.readLine())!=null) System.out.println(line);
  8.         while((line=_error_.readLine())!=null) System.out.println(line);
  9.         System.out.println("Exit Code: " + l_p.waitFor());
  10.  
Here my program hangs, and if i comment the reading of input(output of javac) then it works .how?
Aug 12 '08 #8
JosAH
11,448 Recognized Expert MVP
Josh "hit en end-of-file condition" what does that mean?
And i tried out a code ..but not had fruitful result.
I am using Windows ..now i came to know that runtime.exec behaves OS specific .

Expand|Select|Wrap|Line Numbers
  1. Process l_p = Runtime.getRuntime().exec("javac");
  2.         InputStream output = l_p.getInputStream();
  3.         InputStream error = l_p.getErrorStream();
  4.         BufferedReader _output_ = new BufferedReader(new InputStreamReader(output));
  5.         BufferedReader _error_ = new BufferedReader(new InputStreamReader(error));
  6.         String line = null;
  7.         while((line=_output_.readLine())!=null) System.out.println(line);
  8.         while((line=_error_.readLine())!=null) System.out.println(line);
  9.         System.out.println("Exit Code: " + l_p.waitFor());
  10.  
Here my program hangs, and if i comment the reading of input(output of javac) then it works .how?
I thought you said you understood what I wrote? When you exec just "javac" it
tries to print an helpful page to stderr (try it manually!). You first try to read what
came out of the process' stdout stream (nothing) but that stream doesn't hit any
end-of-file condition; there just isn't any output to stdout so your first loop never
terminates and you never start the second loop.

You need separate threads for the reading on any output stream of the exec'd process.
If you know how the process behaves you can anticipate on its behaviour but in
general you need separate threads for the reading part.

kind regards,

Jos
Aug 12 '08 #9
dmjpro
2,476 Top Contributor
I thought you said you understood what I wrote? When you exec just "javac" it
tries to print an helpful page to stderr (try it manually!). You first try to read what
came out of the process' stdout stream (nothing) but that stream doesn't hit any
end-of-file condition; there just isn't any output to stdout so your first loop never
terminates and you never start the second loop.

You need separate threads for the reading on any output stream of the exec'd process.
If you know how the process behaves you can anticipate on its behaviour but in
general you need separate threads for the reading part.

kind regards,

Jos
Very interesting!
Ok i have some some questions ...
If the stdout is empty then why not it hits the end-of-file condition?
and why do we need 2 process the streams before call waitfor?
please make me understand .... :-)
Aug 12 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3160
by: Bob Murdoch | last post by:
I'm developing an intranet application using W2k server and IE5.5 - 6.x clients. I've got one particular function that calls a stored procedure to update a number of records, depending on user input. The problem we have run into is that this procedure can take a few minutes to complete in certain circumstances, and of course IIS times out. ...
11
4930
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come...
14
18816
by: Des L. Davis | last post by:
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003 Server Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000 Server If you run the code below, you'll notice something odd occuring. The MilliSecond value does not change after a 1Millisecond...
7
3307
by: MLH | last post by:
This was contributed by someone in this NG last year... Sub WaitFor(psngSeconds As Single) ' wait for specified number of seconds ' Copyright Trevor Best (tre...@besty.org.uk) <-OK, so I added thisline. Dim sngStart As Single Dim sngET As Single
2
2227
dmjpro
by: dmjpro | last post by:
I run process on unix through runtime.exec() method. But my process termintaes before chid process terminates. How can I trap that child process termination. The process.waitfor method block for while until the process terminates.. Can anyone tell me how can I get process information randomly until the main process terminates and it's child...
8
9588
by: angelotti | last post by:
Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app....
12
2091
Nepomuk
by: Nepomuk | last post by:
Hi! I'm having a problem - I'm calling an external Process with Process p = Runtime.getRuntime().exec("cmd /c ..."); (of course, without the dots but with a command instead) and have to wait for that Process to be completed before progressing. However, if I try waiting with: p.waitFor();or like this: boolean finished = false;...
6
2433
by: chalani | last post by:
Hi... I was unable to find the implementation of process.java. What i need is the implementation of waitFor() method of process.java. but at every place what i find is the abstract class. could any of you send me an implementation code of the process.java thnkx...
6
16899
by: itsraghz | last post by:
Dear All, I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be executed conditionally through Program A based on the commands it gets from the user. Let's say, we have two inputs, "start" and "stop" to drive the...
0
8065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7823
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8102
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6458
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5303
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.