试图在 bash shell 中捕获 javac 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/317733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:33:15  来源:igfitidea点击:

trying to capture javac output in bash shell

bashshelljavac

提问by jcee14

I'm trying to redirect the java compiler output to a file. I thought it's supposed to be:

我正在尝试将 java 编译器输出重定向到一个文件。我以为应该是:

javac file.java > log.txt

or something. Instead, I see all the output on the terminal and nothing in log.txt!

或者其他的东西。相反,我在终端上看到了所有输出,而在 log.txt 中什么也没有!

Also, if I want to log errors too, do I do

另外,如果我也想记录错误,我做

javac file.java 2>&1 > log.txt

?

?

回答by Julien Oster

javac file.java 2> log.txt

The reason is that you have twooutput file descriptors instead of one. The usual one is stdout, which you can redirect with > and it's supposed to be used for resulting output. The second one, stderr, is meant for human readable output like warnings, errors, current status etc., this one is redirected with 2>.

原因是您有两个输出文件描述符而不是一个。通常的一个是 stdout,你可以用 > 重定向它,它应该用于结果输出。第二个,stderr,用于人类可读的输出,如警告、错误、当前状态等,这个用 2> 重定向。

Your second line, using 2>&1, redirects stderr to stdout and finally stdout into log.txt.

第二行,使用 2>&1,将 stderr 重定向到 stdout,最后将 stdout 重定向到 log.txt。

回答by Bill the Lizard

Have you tried

你有没有尝试过

javac -Xstdout log.txt file.java

This will send compiler errors to a log file instead of stderr.

这会将编译器错误发送到日志文件而不是 stderr。