java version输出_为什么“java -version”将其输出打印到错误流?

根据

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值