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
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
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.