wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to get both ErrorStream and InputStream of a Process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get both ErrorStream and InputStream of a Process" Watch "How to get both ErrorStream and InputStream of a Process" New topic
Author

How to get both ErrorStream and InputStream of a Process

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Dear All,

I have stuck at a place while creating an application. I tell you the situtation.

I am creating a program in which I have to call an external Batch program from my code, for that I am using following lines of code:


Now here the problem comes actually, see above the "str". I beleive this would never run on any JVM as the command given to run is incorrect so it will now execute.. Now here is the main hack, if I am getting the command to be executed from the user, then there would be a possibility to get wrong/correct command. Accordingly I have to show them a message if any command is not running properly. And if command is running properly then I have to show them a sucess message.

Now how can I record both Input and Error Stream of the process simultaneously. When I have tried to record both Error Stream and Input Stream, my application got hanged.

Can anybody throw some light on this issue.
[ September 19, 2007: Message edited by: Ankur Sharma ]

The Best way to predict your future is to create it - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Any updates Moderators/Ranchers.... ?

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The standard article on this is When Runtime. exec () won't, from JavaWorld. Basically you probably need to use two separate threads to consume each stream independantly.

Or, if you're using JDK 5+, you can simplify this a bit using a ProcessBuilder. You can use redirectErrorStream(true) to merge all output into a single stream. You can also do something similar using earlier JDKs if you modify the system command to merge the streams in the first place. Typically this can be done in both Windows and UNIX environments with something like

somecommand 2>&1

which means send stream 2, the error stream, to the same place as stream 1, standard out.

I would sometimes do this instead:

somecommand > outfile.txt 2> errors.txt

which writes stdout and err to different files, oror

somecommand > outfile.txt 2>&1

which writes them both to the same file. Then you don't have any output from the Process at all.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get both ErrorStream and InputStream of a Process
 
Similar Threads
difference b/w 'main method' and 'run on server'...
Input redirection in Java
how to execute commands in servlets
can't run commands from application
why javaw file io dose not work properly