Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

external process and redirecting its streams

3 views
Skip to first unread message

Marteno Rodia

unread,
Aug 4, 2009, 7:25:31 AM8/4/09
to
Hello,
In my program, I want to launch another Java program on another JVM...
The way how I try to do it and the problem I experienced is as
follows:

1) Primary is another class in my project and is executable...

2) I try to start its main method in the following way:
Process proc = Runtime.getRuntime().exec("java Primary");
or:
String[] cmdArray = {"java", "Primary"};
Process proc = Runtime.getRuntime().exec(cmdArray);

3) Then I try to read process' streams in a separate thread.
4) In the main thread program stops at proc.waitFor() and execution
doesn't resume...
5) In the "reading" thread, the function which blocks execution is
BufferedReader.readLine() (I would rather expect it to return null in
the worst case... but I have even used the BufferedReader.ready()
method and I believe I wait if the buffer is empty...)
6) Nothing happens, the program can't finish...

I've been already asking about the apporach I use:
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/11e367a28a0b41fd/9d69aa5ef2e34211
And I know this (very good, in my opinion) article:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

First, I wanted to test notions of visibility between Java executable
classes when invoking my program both from NetBeans IDE and as a JAR
from command line... But I can't do it (as I described above).

What is wrong in my approach?
Thanks in advance for your help.

MR

Patricia Shanahan

unread,
Aug 4, 2009, 9:05:42 AM8/4/09
to
Marteno Rodia wrote:
...

> 3) Then I try to read process' streams in a separate thread.
...

A single thread for both output streams, or a thread for each stream?

Patricia

Marcin Rodzik

unread,
Aug 4, 2009, 10:16:35 AM8/4/09
to
On Aug 4, 3:05 pm, Patricia Shanahan <p...@acm.org> wrote:
> A single thread for both output streams, or a thread for each stream?

A single thread. Now I know that is possible, but tricky! :D

One of the streams were empty all the time and it was blocking! The
main reason was that first I was waiting until any of the streams is
ready and then, incidentally, I was trying to read from unready
stream :)

Now I can ask about my main problem... but in a separate thread!

MR

Patricia Shanahan

unread,
Aug 4, 2009, 12:03:36 PM8/4/09
to
Marcin Rodzik wrote:
> On Aug 4, 3:05 pm, Patricia Shanahan <p...@acm.org> wrote:
>> A single thread for both output streams, or a thread for each stream?
>
> A single thread. Now I know that is possible, but tricky! :D
>
> One of the streams were empty all the time and it was blocking! The
> main reason was that first I was waiting until any of the streams is
> ready and then, incidentally, I was trying to read from unready
> stream :)

To me, this seems like unnecessary trickiness, compared to the cost of
either using two threads, or using ProcessBuilder to combine the streams.

Patricia

0 new messages