3

I want to set output stream to the Command prompt like this:

Process p = Runtime.getRuntime()
              .exec("C:\\Windows\\System32\\cmd.exe /c start cls");
System.setOut(new PrintStream(p.getOutputStream()));

but it is not working, why ?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39

1 Answers1

0

By default, PrintStreams will not flush contents written to them automatically. This means that data you write to it will not be immediately sent to the stream it wraps around. However, if you construct the PrintStream using new PrintStream(p.getOutputStream(), true), it will automatically flush when any of the println methods are invoked, a byte array is written or a newline is written. This way, anything you write to it will be immediately accessible to the process.

See http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html

TheSuccessor
  • 124
  • 7