Why does 'java -version' go to stderr?

扶醉桌前 提交于 2019-11-27 02:04:49

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: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4380614. Note the "Resolution: Wont Fix" ... and the final comment.

The workaround for this problem is:

java -version 2> java_version.txt
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!