-2

i am getting output of following command in error stream instead of input stream

Runtime rt = Runtime.getRuntime();
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd.exe","/c","java -version"});
Process pr =   builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line=input.readLine();
System.out.println(line);

please explain

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
mayur_mitkari
  • 189
  • 1
  • 6
  • 16

2 Answers2

4

Clearly, Java sends the version string into stderr. This is not a very uncommon practice because it is not the output of the client code. Don't be mislead by the name ErrorStream: it is used for much more than errors; it is basically a signalling side-channel beside the main one, which is stdout.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0
It works file in jdk7

    public static void main(String[] args) throws Exception {
        Runtime rt = Runtime.getRuntime();
        ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd.exe","/c","java -version"});
        Process pr =   builder.start();
        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
        String line=input.readLine();
        System.out.println(line);

    }
Biswajit
  • 2,434
  • 2
  • 28
  • 35