exec with stdin stdout

Discussion in 'Java' started by Guest, Sep 6, 2005.

  1. Guest

    Guest 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);
     
    Guest, Sep 6, 2005
    #1
    1. Advertising

  2. Guest

    Guest 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?
     
    Guest, Sep 6, 2005
    #2
    1. Advertising

  3. 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'
     
    Andrew Thompson, Sep 6, 2005
    #3
  4. Guest

    Roedy Green Guest

    On Tue, 06 Sep 2005 19:23:19 +0300, "<- Chameleon ->"
    <> 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.
     
    Roedy Green, Sep 6, 2005
    #4
  5. Guest

    Roedy Green Guest

    On Tue, 06 Sep 2005 19:23:19 +0300, "<- Chameleon ->"
    <> 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.
     
    Roedy Green, Sep 6, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Gessos

    exec with stdin stdout

    Paul Gessos, Sep 6, 2005, in forum: Java
    Replies:
    3
    Views:
    588
    Andrew Thompson
    Sep 6, 2005
  2. Bernard
    Replies:
    4
    Views:
    5,417
    Raymond DeCampo
    Jan 3, 2006
  3. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,461
    Chatoyer
    May 17, 2013
  4. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    824
    Dan Pop
    Jun 21, 2004
  5. Ben
    Replies:
    2
    Views:
    1,409
    jacob navia
    Aug 29, 2009
Loading...

Share This Page