How to read unbuffered stdout of a new process?

Discussion in 'Java' started by uljanow, Aug 9, 2007.

  1. uljanow

    uljanow Guest

    hi,
    is there a way to read directly from stdout of a process? I guess my
    problem is that I'm using InputStreams, but I haven't found another
    way.

    ProcessBuilder builder = new ProcessBuilder("tail", "-f", "foo.log");
    Process child = builder.start();
    Scanner s = new Scanner(child.getInputStream()).useDelimiter("\\n");

    while (!interrupted && s.hasNext())
    log.append(s.nextLine() + "\n");

    The problem with the code example is that the output of tail appears
    after a buffer is filled. The log variable (JTextArea) appends the
    string immediately.

    Regards
    uljanow, Aug 9, 2007
    #1
    1. Advertising

  2. On Thu, 09 Aug 2007 09:09:30 -0700, uljanow wrote:
    > The problem with the code example is that the output of tail appears
    > after a buffer is filled. The log variable (JTextArea) appends the
    > string immediately.


    Any delay you experience is due to the child process, which buffers
    its output. Also, tail -f does not react immediately to changes in the
    file it's monitoring (e.g. GNU tail waits 1 second between updates by
    default).

    You could implement tail -f in Java and avoid the issue altogether.

    /gordon

    --
    Gordon Beaton, Aug 9, 2007
    #2
    1. Advertising

  3. uljanow

    Roedy Green Guest

    >The problem with the code example is that the output of tail appears
    >after a buffer is filled. The log variable (JTextArea) appends the
    >string immediately.

    If you can modify the child code, turn off any System.err and
    System.out buffering, and do .flush periodically to send the output
    off to the waiting mother task.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 9, 2007
    #3
  4. "uljanow" <> wrote in message
    news:...
    > hi,
    > is there a way to read directly from stdout of a process? I guess my
    > problem is that I'm using InputStreams, but I haven't found another
    > way.
    >
    > ProcessBuilder builder = new ProcessBuilder("tail", "-f", "foo.log");
    > Process child = builder.start();
    > Scanner s = new Scanner(child.getInputStream()).useDelimiter("\\n");
    >
    > while (!interrupted && s.hasNext())
    > log.append(s.nextLine() + "\n");
    >
    > The problem with the code example is that the output of tail appears
    > after a buffer is filled. The log variable (JTextArea) appends the
    > string immediately.


    To add to the previous responses, many command line programs
    will buffer output when they are connected to a pipe, but not
    when connected to a terminal or console. This makes it
    difficult to run an interactive program through a Java launcher
    program that provides the I/O as, for example, an IDE might do.

    In UNIX/Linux, you can insert a native program between Java
    and the target that connects to the target using a pty, and feeds
    the target's output to the Java program as soon as it gets it. The
    "wedge" code that is distributed with jGRASP provides that
    function (and others) and is free of license restrictions. On
    Windows, the situation is more difficult. There is no
    "pseudoconsole". You can sort-of build one on NT/XP by
    running the target in debug mode and intercepting console
    writes, but it is difficult to distinguish input from output, and
    you have to give up if there is any "cursor movement". I used
    Gordon Chaffee's Windows port of "Expect" as a guide for
    building such a program.
    Larry Barowski, Aug 10, 2007
    #4
    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. Elad
    Replies:
    0
    Views:
    409
  2. Yang Zhang

    Unbuffered stdout/auto-flush

    Yang Zhang, Jun 21, 2008, in forum: Python
    Replies:
    1
    Views:
    439
  3. Andreas S
    Replies:
    3
    Views:
    257
    Eric Hodel
    Dec 9, 2006
  4. Iñaki Baz Castillo

    How to read a PIPE in unbuffered way?

    Iñaki Baz Castillo, Sep 24, 2008, in forum: Ruby
    Replies:
    3
    Views:
    191
    Iñaki Baz Castillo
    Sep 25, 2008
  5. sahana H V
    Replies:
    3
    Views:
    283
    Josef Moellers
    Jul 10, 2009
Loading...

Share This Page