0

Why does this :

Runtime rt = Runtime.getRuntime();
String cmd = "cmd /c java -version";
Process process = rt.exec(cmd);
process.waitFor();

StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

String line;           
while ((line = reader.readLine())!= null) {
    output.append(line + "\n");
} 
System.out.println(output);
} catch (Exception e) {
    logger.error(e);
}

print into the error stream and not in the standard input stream? What is erroneous about the command java -version?

N.B. The cmd /c is because I am on a Windows machine

  • what's the error message? –  Jan 21 '16 at 13:44
  • What is written to the error stream? – StephaneM Jan 21 '16 at 13:44
  • 1
    `java -version` is always printed on the error stream. It's not especially wrong in itself, it's a design choice. – Olivier Grégoire Jan 21 '16 at 13:44
  • `java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)` It is not an error message, just outputs the Java version. But why does it do it in an Error stream and not in a standard Input stream? –  Jan 21 '16 at 13:45
  • I had lots of issues with that kind of execution (error codes, stoud/stderr, timouts, ...) We now use `apache-exec` lib. If you can, try it. – Patrick Ferreira Jan 21 '16 at 13:46
  • 1
    I guess you need to put process.getInputStream() instead of process.getErrorStream (or create a BufferedReader for each of them if you need both). I would however recommend using a ProcessBuilder, from which you can get a Process object and access directly to its std/err streams. – bmorin Jan 21 '16 at 14:20

0 Answers0