Write to a process once its started using getOutputStream()

M

michael.shnitzer

I have a dos program that I wish to access through my java program... I
can run the program and read the programs output using

DataInputStream in = new DataInputStream(new BufferedInputStream
process.getInputStream()));

But when I try to write to the program using...

DataOutputStream out = new DataOutputStream(new
BufferedOutputStream(process.getOutputStream()));
out.write(command.getBytes());
out.flush();

I get no response from the program and it doesn't seem to respond to
the command.

I am trying this on xp.
 
L

Lee Fesperman

I have a dos program that I wish to access through my java program... I
can run the program and read the programs output using

DataInputStream in = new DataInputStream(new BufferedInputStream
process.getInputStream()));

But when I try to write to the program using...

DataOutputStream out = new DataOutputStream(new
BufferedOutputStream(process.getOutputStream()));
out.write(command.getBytes());
out.flush();

I get no response from the program and it doesn't seem to respond to
the command.

I am trying this on xp.

You didn't show the contents of command. Perhaps the program needs a newline.
 
M

michael.shnitzer

It wasn't a new line problem... i tried new line and i have the flush
in there...

What is the correct way to use the getOutputStream of the process...
which is the stdin of the program opened by the process I assume.
 
L

Lee Fesperman

It wasn't a new line problem... i tried new line and i have the flush
in there...

What is the correct way to use the getOutputStream of the process...
which is the stdin of the program opened by the process I assume.

You need to do getOutputStream and getInputStream in separate threads. You may also need
to do getErrorStream (in yet another thread).

Have you tried writing a simple Java console application and calling that?
 
J

John Currier

I get no response from the program and it doesn't seem to respond to
the command.

What exactly do you mean by that statement? Does your program block
when it does a process.waitFor()?

If there's a chance that the program you're executing will send
anything to stdout or stderr then you'll need to create a thread to
read from the stream(s) that it's writing to (process.getInputStream()
/ process.getErrorStream()) as Lee mentioned. Note that if you don't
and if the program writes more data than the size of the output buffers
then the program will block waiting for someone to read from the other
side of those buffers...and nobody's there to read from it...deadlock.

John
http://schemaspy.sourceforge.net
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top