NIO and stdout

H

Horst Walter

I want to run an external program (Runtime:exec()) and capture the
output (System.out / System.err) by NIO.

All examples I found on the web were about NIO and files and NIO and
ByteBuffers. Is there no easy way to accomplish this? An example would
really help.

Thanks
HW
 
G

Gordon Beaton

I want to run an external program (Runtime:exec()) and capture the
output (System.out / System.err) by NIO.

All examples I found on the web were about NIO and files and NIO and
ByteBuffers. Is there no easy way to accomplish this? An example would
really help.

I'm not sure that you can, there should be a method like
InputStream.getChannel() otherwise. I don't think you can use NIO to
read from the Java application's own stdin (System.in) either. Both
should have been included, IMO.

It seems like AbstractSelectableChannel could be subclassed, but I
don't see how you would implement the selection operations on an
existing InputStream.

If you have an existing framework and want to use it to read input
from the child process along with other things, then you could use a
thread to read from the child process in the traditional way, and pass
the data using a java.nio.channels.Pipe to the NIO selection loop.

/gordon
 
S

Sudsy

Gordon Beaton wrote:
If you have an existing framework and want to use it to read input
from the child process along with other things, then you could use a
thread to read from the child process in the traditional way, and pass
the data using a java.nio.channels.Pipe to the NIO selection loop.

Good call, Gordon. After digging into the javadocs, that seems like the
right approach. Kind of annoying that you still have to fire up two
extra threads, however.
 

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

Similar Threads

RandomAccessFile and java.nio.* 14
JAVA NIO 4
NIO enigma 4
Using NIO 2
Java NIO server and Python asyncore client 2
Question about non-blocking NIO and Selection Keys 12
NIO CPU Anomaly 3
Help with NIO 4

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top