4

I'm trying to spawn a new console in java and get the ouput stream.

I tried this way :

Process p = Runtime.getRuntime().exec("cmd.exe /c start");
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(
                                         p.getOutputStream()));

the console spawn but i'm not able to write something on the stream !

The other way :

Process p = Runtime.getRuntime().exec("cmd.exe");
  BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
               p.getOutputStream()));

This time i can write to the stream, but the console is not spawning !

I lack knowledge :/

Thank you in advance.

  • I'm not an expert on consoles. From what I can tell from experimentation, however, when the first example, first line is run, cmd is opened and awaits a system command. Since Writers depend on the java virtual machine running on a specific output area, I don't think you can output to the new cmd at all since it isn't running any form of virtual machine. If you want to open a cmd window that is simply an extension of the original (doesn't show the Windows version or anything, just a black screen), that might be an idea to work off of. – snickers10m Apr 27 '15 at 01:29
  • ..and if you run **the other way** from `cmd`(.bat), it feels like very subtle difference. What would justify such an effort (and I think, also without further references, just my pre-commenter: It will be painful to impossible to get that outputsream) ... with "this way". – xerx593 Apr 27 '15 at 01:47

1 Answers1

0

In your first way you started cmd and it started another process which you have no access too. Don't use this way if you need input/output stream.

The other way is right. Process is running fine. But you didn't provide any input for it. Obtain input stream and send some commands like "cd foo\n", "dir\n". As you can see this is regular stream so it requires to execute the command. Then you can use output stream too.

There are a lot of examples how to do so.

Community
  • 1
  • 1
Izold Tytykalo
  • 719
  • 1
  • 5
  • 15