Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

How to read unbuffered stdout of a new process?

Thread view: 
uljanow - 09 Aug 2007 17:09 GMT
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
Gordon Beaton - 09 Aug 2007 18:23 GMT
> 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

--
Roedy Green - 09 Aug 2007 20:00 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Larry Barowski - 10 Aug 2007 15:59 GMT
> hi,
> is there a way to read directly from stdout of a process? I guess my
[quoted text clipped - 11 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.