Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching InputStream from Runtime/Process

 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this is a Microsoft only deployment.
 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic