This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Catching InputStream from Runtime/Process Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Catching InputStream from Runtime/Process" Watch "Catching InputStream from Runtime/Process" New topic
Author

Catching InputStream from Runtime/Process

Prashant Sehgal
Ranch Hand

Joined: Jun 20, 2003
Posts: 56
I have a .cmd file - say - with the following code:


I want to invoke this using Runtime.getRuntime().exec(.) and catch the "I am a command that produces a lot of text output" text in the InputStream being returned from the Process.

The problem is that I *must* start this command in a separate DOS window. In doing that, my program loses the handle to the stream and does not return any text from the command file.

So, how do I start the command in a separate window while still being able to catch all it's output in my Process's inputstream?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Originally posted by Prashant Sehgal:
The problem is that I *must* start this command in a separate DOS window.


By using a DOS window, are you tying yourself to a particular operating system? You need to read Michael Daconta's classic article (Google for "when Runtime.exec() won't") before even thinking of using Runtime.exec(). That tells you how to get all the output from the Streams, and the newer ProcessBuilder class makes that easier, but I think your output will be redirected to the original terminal window.
Prashant Sehgal
Ranch Hand

Joined: Jun 20, 2003
Posts: 56
Yes this is a Microsoft only deployment.
Prashant Sehgal
Ranch Hand

Joined: Jun 20, 2003
Posts: 56
The output is not getting redirected to the original terminal window. I anyway want the output to display in the a new terminal window and also to catch it in the Processes's inputstream. I'll check out the ProcessBuilder class and the article you mention as well.

But my original problem stands.

How to catch the inputstream from a new DOS window that has been created by "exec()ing" a .cmd script from within your java program.
Prashant Sehgal
Ranch Hand

Joined: Jun 20, 2003
Posts: 56
This might illustrate better:

Java Code:


Cmd Script: test_1.cmd


Bat Script:


I want my original program to return "this is test 2" in it's inputstream. Is it even possible?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Catching InputStream from Runtime/Process
 
Similar Threads
Running Batch Files
Console Output of a command to file
Help on sequential method execution
How to call BAT file from JAVA programs ?
Need to run java processes in same session