How to pipe process output to System.out?

J

Jacob

I create a Process by

Process process = Runtime.getRuntime().exec ("program");

How can I connect the output output/error stream
of this process to Systsem.out/err?

Thanks!
 
T

Thomas Weidenfeller

Jacob said:
I create a Process by

Process process = Runtime.getRuntime().exec ("program");

How can I connect the output output/error stream
of this process to Systsem.out/err?

Check the API documentation of Process for a start.

/Thomas
 
J

Jacob

Thomas said:
Check the API documentation of Process for a start.

Thomas: If you cant contribute, then don't.
There is far too much spam already.

I know Process has a getOutputStream() method.
To you other guys: How can I connect this to
my System.out stream?

Thanks.
 
G

Gordon Beaton

I know Process has a getOutputStream() method. To you other guys:
How can I connect this to my System.out stream?

If you want the output of the child process to automatically go to the
same place the output of the parent process goes, you're asking for
something that java can't really do.

You need to "manually" read() from the childs output and write() it to
System.out.

Alternatively, and since you seem to be using Linux, you could do
something system-specific like this:

String[] cmd = { "/bin/sh", "-c", "mycommand > /dev/tty" };
Runtime.getRuntime().exec(cmd);

This assumes that the *parent* stdout hasn't been redirected.

/gordon
 
T

Thomas Fritsch

Gordon said:
If you want the output of the child process to automatically go to the
same place the output of the parent process goes, you're asking for
something that java can't really do.

You need to "manually" read() from the childs output and write() it to
System.out.
Just a common pitfall to mention:
You should do the shuffling from process.getInputStream() to System.out
in a separate thread.
Otherwise you risk getting a deadlock between java and the external process,
especially when that process generates large output on stdout.
(writing to stdout is blocked by the OS, when the pipe buffer is full,
and the other side doesn't read it)
Alternatively, and since you seem to be using Linux, you could do
something system-specific like this:

String[] cmd = { "/bin/sh", "-c", "mycommand > /dev/tty" };
Runtime.getRuntime().exec(cmd);

This assumes that the *parent* stdout hasn't been redirected.

/gordon
______________________________________
Thomas<dot>Fritsch<squiggle>ops<dot>de
 
T

Thomas Weidenfeller

Jacob said:
Thomas: If you cant contribute, then don't.
There is far too much spam already.

You have no idea what spam is.
I know Process has a getOutputStream() method.

Then use it.

BTW: No need for you to reply. I won't see it.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top