-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
  • 49,592
  • 20
  • 90
  • 140
mayur_mitkari
  • 169
  • 1
  • 5
  • 13

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
  • 188,298
  • 27
  • 302
  • 416
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,376
  • 2
  • 25
  • 35