Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'java -version' go to stderr?

Tags:

Is there any special reason for the results of java -version going to stderr?

For example, this command executed from Windows' prompt line:

java -version > java_version.txt 

leaves the file java_version.txt empty.

EDIT: The same happens with the help printed out after executing java.exe without any parameters.

EDIT: Just out of a sheer curiosity I checked whether it has been always like that and it turned out it actually has. java -version goes to stderr in JDK 1.1.8 and also in JDK 1.2.2, however the outputs of java.exe without any parameters do not.

like image 638
Jagger Avatar asked Nov 20 '12 22:11

Jagger


People also ask

What is stderr in Java?

Standard error (stderr), which normally also goes to the console. Exposed as System. err.

What do you use stderr for?

Stderr is the standard error message that is used to print the output on the screen or windows terminal. Stderr is used to print the error on the output screen or window terminal. Stderr is also one of the command output as stdout, which is logged anywhere by default.


1 Answers

Is there any special reason for the results of java -version going to stderr?

AFAIK, there is no special reason. It is just how the java command was / is implemented. Probably all the way back to Java 1.0, though it would be very difficult to verify that.

My brief investigation shows that this behavior is inconsistent with how most Linux commands behave ... everything else I've tried uses stdout for version information. (After all, the version information is not "error" output.)

Note however --version / -version options are a convention rather than something required by any formal standard. (The GNU coding standards state that commands should implement --version and that version info should be written to standard output. But POSIX standards don't mention this, nor do the LSB standards.)


What can / should you do?

  • It should be easy to capture stderr instead of stdout in your shell script or batch file.
  • There shouldn't be any risk in doing this. Oracle cannot change the Java tool chains to send -version output to stdout without potentially breaking customer scripts. This is highly unlikely1.

1 - Here is evidence of just how unlikely it is: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4380614. Note the "Resolution: Wont Fix" ... and the final comment.

like image 144
Stephen C Avatar answered Sep 23 '22 08:09

Stephen C