exec with stdin stdout

G

Guest

Why this code not working?


byte[] buf = new byte[10000];
Process p = Runtime.getRuntime().exec("php -n 1.php");
p.getErrorStream().close();
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("write serialized data...".getBytes());// never takes data
p.waitFor();
int count = is.read(buf); // count == -1
System.out.println(buf); // write garbage
System.exit(0);
 
G

Guest

Why this code not working?
byte[] buf = new byte[10000];
Process p = Runtime.getRuntime().exec("php -n 1.php");
p.getErrorStream().close();
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("write serialized data...".getBytes());// never takes data
p.waitFor();
int count = is.read(buf); // count == -1
System.out.println(buf); // write garbage
System.exit(0);


So! it works now! The output comes to ErrorStream (from my error)
I correct the problem and the output comes to InputStream.

But the
os.write("write serialized data...".getBytes());
never gives the data to child process.
Why?
 
A

Andrew Thompson

So! it works now!

Aaah.. There you go - not lazy, just resting. ;-)

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"..my time is all used up, just to sit around creatin¢ all that groovy kind
of stuff."
Spencer Davis Group 'I'm A Man'
 
R

Roedy Green

Process p = Runtime.getRuntime().exec("php -n 1.php");
p.getErrorStream().close();
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("write serialized data...".getBytes());// never takes data
p.waitFor();

What does the spawned program do? If it writes first, it is sitting
waiting for you to accept those bytes before it will read the ones you
are sending it, in a Mexican standoff.

Either you need to choreograph the dance carefully, or use separate
threads to read and write.
 
R

Roedy Green

os.write("write serialized data...".getBytes());// never takes data

I trust this spawned C program is reading 8-bit chars from the console
in the default character encoding, and is not waiting for a \n or the
like.

It is surely not reading Java serialised data. Only Java could make
head nor tail of that.

For more hints see http://mindprod.com/jgloss/exec.html
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top