Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

opening a new cmd and writing to it

 
Bhargo Dev Arya
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bhargo Dev Arya
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys......
I really appreciate all your help......
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic