• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Catching InputStream from Runtime/Process

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this is a Microsoft only deployment.
 
Prashant Sehgal
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic