• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

How to get both ErrorStream and InputStream of a Process

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any updates Moderators/Ranchers.... ?

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic