File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes opening a new cmd and writing to it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "opening a new cmd and writing to it" Watch "opening a new cmd and writing to it" New topic
Author

opening a new cmd and writing to it

Bhargo Dev Arya
Greenhorn

Joined: Jun 29, 2011
Posts: 8

Hi
I am trying to open a new command prompt at my screen from my java program...
I have been able to do it but now i want to write in that new command prompt from the same java program...
IS IT POSSIBLE???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Don't know. Please show us how you did that (even though that may be Windows®-specific code which is contrary to the philosophy of platform-independence). Then we can see whether your code can be extended to write to the other command prompt.

And welcome to the Ranch
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

You can use the Runtime class and execute any command. An example:



This will execute the command, but not make you able to read output and write new commands.
But the call returns a Process that you could use...

Another way is to use the java.lang.ProcessBuilder.
That class has the ability to redirect the in and out streams. Take a look at http://www.devdaily.com/java/java-exec-processbuilder-process-1.
Adam Outler
Greenhorn

Joined: Jun 24, 2011
Posts: 18
Runtime has problems. use ProcessBuilder



Pass in the command as the first object in the array and all parameters come after... ie..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Even when using ProcessBuilder you must still empty both Streams from the Process. I suspect that code will block because you are reading the whole of the error stream before you finish the output stream. Google for When Runtime.exec() won't Michael Daconta; the article might be old, but it is still relevant.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Adam Outler wrote:Runtime has problems. use ProcessBuilder


Any problems are likely of your making since when ProcessBuilder was introduced (JDK 1.5 ?) Runtime.exec() was re-engineered so as to use ProcessBuilder behind the scenes. In general, I find ProcessBuilder has a couple of features that make it easier to use than Runtime.exec().

Getting back to the OP's original problem. I have been thinking about this for some time I'm not convinced one can do what the OP wants. One can write to the cmd.exe Process stdin but this is interpreted by cmd.exe and not just echoed to the output window. In the article I am writing on Runtime.exec() and ProcessBuilder I bypass the problem and just write the output to a JTextArea.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Campbell Ritchie wrote:Even when using ProcessBuilder you must still empty both Streams from the Process.

Except when you use redirectErrorStream(true), as that will cause the error stream to be merged with the output stream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bhargo Dev Arya
Greenhorn

Joined: Jun 29, 2011
Posts: 8

Thanks for the replies guys......
I really appreciate all your help......
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Sometimes, I wish that this was not possible. Just code reviewed a piece of code where the developer used the Runtime to look for the OS-type and then did a file copy...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Rob Spoor wrote: . . . Except when you use redirectErrorStream(true), as that will cause the error stream to be merged with the output stream.
That is simply a way of emptying both Streams in one action.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: opening a new cmd and writing to it
 
Similar Threads
new command window
Can not create java virtual machine
Launching telnet using java program fails with Win 7 OS
How to get DNS name and IP Address with Java code
Running Java Programs from the command prompt