469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 11167
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

Post your reply

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

Similar topics

2 posts views Thread by Bob Murdoch | last post: by
14 posts views Thread by Des L. Davis | last post: by
Nepomuk
12 posts views Thread by Nepomuk | last post: by
6 posts views Thread by chalani | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.