473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A question about Process.waitFor

dmjpro
2,476 2GB
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 11621
JosAH
11,448 Expert 8TB
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 Expert 2GB
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 2GB
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 Expert 8TB
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 2GB
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 Expert 8TB
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 2GB
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 Expert 8TB
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 2GB
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
JosAH
11,448 Expert 8TB
Very interesting!
Ok i have some some questions ...
If the stdout is empty then why not it hits the end-of-file condition?
Because some output (i.e. input to your Java program) can come through later.

and why do we need 2 process the streams before call waitfor?
please make me understand .... :-)
As I wrote several times before: those other threads can read from those stdout
and stderr streams as long as that external process still lives. When it dies those
threads will die with it if they loop while not end-of-file. In the mean time your
main thread can safely join the thread for that other process.

kind regards,

Jos
Aug 12 '08 #11
dmjpro
2,476 2GB
Still i m confused ..why do we need to process those streams .... if i don't process why my program hangs ?
please!
Aug 12 '08 #12
JosAH
11,448 Expert 8TB
Still i m confused ..why do we need to process those streams .... if i don't process why my program hangs ?
please!
Because those stream buffers fill up if they don't get emptied by reading from them.
The entire process waits until there is room in those buffers again to write some
more. Those separate threads take care of the emptying of those buffers.

kind regards,

Jos
Aug 12 '08 #13
dmjpro
2,476 2GB
Because those stream buffers fill up if they don't get emptied by reading from them.
The entire process waits until there is room in those buffers again to write some
more. Those separate threads take care of the emptying of those buffers.

kind regards,

Jos

Now the Picture totally came 2 me clearly :-)
Now i ll be reading this link
If any thing i got stuck then i ll be knocking you ....
Thanks a lot .....now i ll be enjoying the Runtime.exec ....
Aug 12 '08 #14
dmjpro
2,476 2GB
Because those stream buffers fill up if they don't get emptied by reading from them.
The entire process waits until there is room in those buffers again to write some
more. Those separate threads take care of the emptying of those buffers.

kind regards,

Jos
Jos one more thing i was going to ask you but i forget to ask ...:-(
When the sub-process executes (using runtime.exec) then does it use for redirecting the streams , System.out,System.err and System.in ?
Aug 13 '08 #15
JosAH
11,448 Expert 8TB
Jos one more thing i was going to ask you but i forget to ask ...:-(
When the sub-process executes (using runtime.exec) then does it use for redirecting the streams , System.out,System.err and System.in ?
Read that article you mentioned; it explains it all; you should've read it before
you asked your question here because it says exactly what I told you.

kind regards,

Jos
Aug 13 '08 #16
dmjpro
2,476 2GB
Read that article you mentioned; it explains it all; you should've read it before
you asked your question here because it says exactly what I told you.

kind regards,

Jos
I read that but basically the basics what i needed ....that's i got from u ... :-)
Aug 13 '08 #17
javaV
1
dude i am facing this hanging problem now. As you have said i read the output and error file. but my program gets hanged
still
I used the below lines only as you have used

InputStream output1 = process.getInputStream();
InputStream error1 = process.getErrorStream();
BufferedReader _output_ = new BufferedReader(new InputStreamReader(output1));
BufferedReader _error_ = new BufferedReader(new InputStreamReader(error1));

System.out.println("Exit Code: " + process.waitFor());
Aug 28 '11 #18

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

Similar topics

2
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...
11
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.,...
14
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...
7
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...
2
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...
8
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...
12
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...
6
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...
6
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.