Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > exec with stdin stdout

Reply
Thread Tools

exec with stdin stdout

 
 
Guest
Posts: n/a
 
      09-06-2005
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);
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      09-06-2005
> 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?
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-06-2005
On Tue, 06 Sep 2005 20:23:06 +0300, <- Chameleon -> wrote:

> 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'
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-06-2005
On Tue, 06 Sep 2005 19:23:19 +0300, "<- Chameleon ->"
<(E-Mail Removed)> wrote or quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-06-2005
On Tue, 06 Sep 2005 19:23:19 +0300, "<- Chameleon ->"
<(E-Mail Removed)> wrote or quoted :

>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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
Runtime.exec on Win98: stdin, stdout with long DOS command line? Bernard Java 4 01-03-2006 02:33 PM
exec with stdin stdout Paul Gessos Java 3 09-06-2005 06:35 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments