简体   繁体   English

CMD:如何将Java -version的结果写入/打印到某些文件

[英]CMD: How to write/print result of java -version to some file

I try to execute: 我尝试执行:

    cmd /c java -version > someFile.txt
    cmd /c echo java -version > someFile.txt

But nothing is written to the file I'm redirecting to. 但是没有任何内容写入到我重定向到的文件中。

Capture stderr instead of stdout: 捕获stderr而不是stdout:

java -version 2> someFile.txt
java -fullversion 2> someFile.txt
cmd /c java -version 2> someFile.txt

This explains why Java writes the help to stderr: 解释了Java为什么将帮助写入stderr的原因:

We should think very, very carefully before ever fixing this bug. 修复此错误之前,我们应该非常非常仔细地考虑。 It's obviously the wrong thing to print version information to stderr, but since we've been doing that since the beginning of time it seems likely that we'll break existing systems built on top of Java if we change it now. 将版本信息打印到stderr显然是错误的事情,但是由于自从开始以来我们就一直在这样做,所以如果现在更改它,很可能会破坏基于Java的现有系统。 If we do decide to change this then it must wait until the Tiger release so that adequate testing can be done. 如果我们决定更改此设置,则必须等到Tiger发行后才能进行足够的测试。

Java writes this information to Standard Error not Standard Output so you need to redirect the correct channel Java将此信息写入标准错误而不是标准输出,因此您需要重定向正确的通道

cmd /c java -version 2> someFile.txt

This works. 这可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM