Write to a process once its started using getOutputStream()

Discussion in 'Java' started by michael.shnitzer@gmail.com, Jun 26, 2005.

  1. Guest

    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.
     
    , Jun 26, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > 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.

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Jun 27, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jun 27, 2005
    #3
  4. wrote:
    >
    > 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?

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Jun 27, 2005
    #4
  5. John Currier Guest

    > 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
     
    John Currier, Jun 29, 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. Reid Madsen
    Replies:
    8
    Views:
    4,522
    Reid Madsen
    May 16, 2004
  2. DiscoStu
    Replies:
    6
    Views:
    794
    DiscoStu
    Jul 9, 2004
  3. thunk
    Replies:
    1
    Views:
    347
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    522
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    655
    thunk
    Apr 3, 2010
Loading...

Share This Page