Why does java -version go into stderr?

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

For example, this command is executed from the Windows prompt line:

 java -version > java_version.txt 

leaves the java_version.txt file empty.

EDIT: The same thing happens with help printed after java.exe run without any parameters.

EDIT: Just because of sheer curiosity, I checked if this was always the case, and it turned out that it really is. java -version goes into stderr in JDK 1.1.8, as well as in JDK 1.2.2, however, java.exe outputs do not work without any parameters.

+11
java stdout stderr
source share
2 answers

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

AFAIK, there is no particular reason. This is how the java command was / implemented. Everything will probably return to Java 1.0, although it would be very difficult to verify this.

My brief research shows that this behavior is incompatible with the way most Linux commands behave ... everything I tried uses stdout for version information. (In the end, version information is not the result of an β€œerror.”)

Please note, however --version / -version parameters are an agreement, not something required by any formal standard. ( GNU coding standards report that commands must implement --version , and this version information must be written to standard output. But POSIX standards do not mention this, as well as LSB standards.)


What can / should you do?

  • It is difficult to remove stderr instead of stdout in your shell script or batch file.
  • There should be no risk. Oracle cannot change the Java -version to send -version output to stdout without potentially breaking client scripts. This is unlikely 1 .

1 - This indicates how unlikely it is: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4380614 . Take a look at "Permission: Wont Fix" ... and the final comment.

+14
source share

Workaround for this problem:

 java -version 2> java_version.txt 
+3
source share

All Articles