简体   繁体   English

Java:为什么在Java / Groovy中运行命令时此命令的输入错误

[英]Java: Why the input of this command is error when run the command in Java/Groovy

The code is: 代码是:

Process p = "java -Xmx128M -version".execute()
Process p = "ls\ndir".execute()
p.inputStream.eachLine { println it}
println "error:"
p.errorStream.eachLine { println it}

then it print: error: 然后打印:错误:

java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode)

Why the output is in the errorStream, not the inputStream? 为什么输出在errorStream中,而不在inputStream中?

I agree that it's not an error to ask java to print its version, but it's just the decision they made from the beginning. 我同意要求Java打印其版本不是错误,但这只是他们从一开始就做出的决定。 I think most applications print version to stdout, but at least a few print it to stderr; 我认为大多数应用程序会将版本打印到stdout,但至少有一些将其打印到stderr。 eg python and gcc seem to print to stderr, ruby prints to stdout. 例如python和gcc似乎可以打印到stderr,ruby可以打印到stdout。

It doesn't seem likely that they will change it, since a bug report got this reply (in 2001): 他们似乎不太可能更改它,因为错误报告得到了此答复(在2001年):

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的现有系统。

So they acknowledge that it's probably the wrong thing to do, but won't change it. 因此,他们承认这样做可能是错误的事情,但不会改变。 The bug is marked with "won't fix". 该错误标记为“无法修复”。

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

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