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 / September 2006

Tip: Looking for answers? Try searching our database.

Process p  = Runtime.getRuntime().exec(String cmd) not reading input stream on Linux

Thread view: 
nmlaney@gmail.com - 20 Sep 2006 21:12 GMT
I'm attempting to use this code on Linux, and only an empty String is
being logged. I tried this with String line = input.readline() and I'm
getting other debug stmts. I get the response on Windows, but not
Linux. What am I missing?

               Process p = Runtime.getRuntime().exec(cmd);
               BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
               int ch;
               StringBuffer sb = new StringBuffer("");
               while ((ch = input.read()) != -1) {
                   sb.append(ch);
               }
               logger.debug(sb.toString());
               input.close();
Matt Humphrey - 20 Sep 2006 21:54 GMT
> I'm attempting to use this code on Linux, and only an empty String is
> being logged. I tried this with String line = input.readline() and I'm
[quoted text clipped - 11 lines]
>                logger.debug(sb.toString());
>                input.close();

There could be several things wrong here, although if you're sure it writes
a blank entry to the log (ie timestamp and no data) it seems likely that the
input stream actually contains nothing and does complete.  It would guess
that it's giving you an error message on stderr which you are not showing.
It's probably saying it can't find the program (exec has different $PATH),
or that you're expecting the cmd to perform a shell operation (e.g. piping
output, etc).  Find out what the error message says and show us what you're
putting into cmd.

In general you must read both stdout and stderr simultaneously (google for
StreamGobbler) because if either fills the buffer it will block and create
deadlock as your program will be waiting for more input.  This is in the
Java Gotcha's.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
danharrisandrews@gmail.com - 20 Sep 2006 21:54 GMT
> I'm attempting to use this code on Linux, and only an empty String is
> being logged. I tried this with String line = input.readline() and I'm
> getting other debug stmts. I get the response on Windows, but not
> Linux. What am I missing?

It has been awhile but I remember needing to read both stdin and
stderr. I've done a quite google and I'm going to direct you to this
link (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)
for starters. The reading in the good example is done in separate
threads for stdin and stderr. Good luck.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Gordon Beaton - 21 Sep 2006 07:05 GMT
> I'm attempting to use this code on Linux, and only an empty String
> is being logged. I tried this with String line = input.readline()
> and I'm getting other debug stmts. I get the response on Windows,
> but not Linux. What am I missing?

Others have already addressed the need to read from both streams
(output and error). I'll add that ProcessBuilder lets you combine them
into one, which could make things easier for the Java application.

Also, I'm curious what your command line looks like. Do any of the
command arguments contain spaces or otherwise need quoting? Are you
passing wildcards to the command, or doing anything else that requires
a shell? Are you starting a shell? Post a concrete example.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



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.