根据
the Java documentation:
-showversion
Displays version information and continues execution of the application. This option is equivalent to the -version option except that the latter instructs the JVM to exit after displaying version information.
-version
Displays version information and then exits. This option is equivalent to the -showversion option except that the latter does not instruct the JVM to exit after displaying version information.
Standard error is another output stream typically used by programs to output error messages or diagnostics.
大多数以POSIX为中心的工具都为其stdout流规定了非常仔细的规范,因此工具的输出可以在其他地方输出. Stderr的规定要少得多,并且可以自由地用于记录和错误.
通过-showversion,Java允许将版本信息打印在正在运行的应用程序旁边.但是,如果将版本信息打印到stdout,它将与可能伴随的正常应用程序输出无法区分,从而迫使您自己扫描并删除该行输出.当然,使用-version而不是-showversion,版本字符串很可能是预期的输出,但保持一致性本身就是一个不错的结局.
要解决此问题,只需将stderr重定向到stdout:
VERSION=$(java -version 2>&1)