Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to read unbuffered stdout of a new process?

Reply
Thread Tools

How to read unbuffered stdout of a new process?

 
 
uljanow
Guest
Posts: n/a
 
      08-09-2007
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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-09-2007
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

--
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-09-2007
>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
 
Reply With Quote
 
Larry Barowski
Guest
Posts: n/a
 
      08-10-2007

"uljanow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to make STDOUT unbuffered, please guide me sahana H V Perl Misc 3 07-10-2009 06:29 AM
How to read a PIPE in unbuffered way? IƱaki Baz Castillo Ruby 3 09-25-2008 10:02 PM
Unbuffered stdout/auto-flush Yang Zhang Python 1 06-22-2008 12:26 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM



Advertisments