代码是:
Process p = "java -Xmx128M -version".execute()
Process p = "ls\ndir".execute()
p.inputStream.eachLine { println it}
println "error:"
p.errorStream.eachLine { println it}
然后打印: 错误:
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)
为什么输出在errorStream,而不是inputStream?
最佳答案
我同意要求 java 打印其版本不是错误,但这只是他们从一开始就做出的决定。我认为大多数应用程序将版本打印到 stdout,但至少有一些应用程序将它打印到 stderr;例如python 和 gcc 似乎打印到 stderr,ruby 打印到 stdout。
他们似乎不太可能改变它,因为 bug report得到了这个回复(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.
所以他们承认这可能是错误的做法,但不会改变它。该错误标记为“不会修复”。
关于Java: 为什么在 Java/Groovy 中运行该命令时该命令输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19290520/