Java System.err 的重点是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21197737/
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
What is the point of System.err?
提问by Gavin Z.
In UNIX, I'm supposed to write a Java file that will print "EXIT 1" to the standard error, and then exit with a status of 1.
在 UNIX 中,我应该编写一个 Java 文件,将“EXIT 1”打印到标准错误,然后以状态 1 退出。
Here is my approach..
这是我的方法..
System.err.println("EXIT 1");
System.exit(1);
Is this what I'm supposed to do?
这是我应该做的吗?
If so, how am I supposed to use it in the Unix shells? When I compile and run it in the bash, it just prints "EXIT 1" (so it does the same thing as System.out.println, why should I use "err"?). What is the "standard error" here?
如果是这样,我应该如何在 Unix shell 中使用它?当我在 bash 中编译并运行它时,它只打印“EXIT 1”(所以它与 System.out.println 做同样的事情,为什么我应该使用“err”?)。这里的“标准错误”是什么?
采纳答案by MultiplyByZer0
Every running program has these three streams:
每个正在运行的程序都有这三个流:
- Standard input (stdin), which normally comes from the keyboard. Exposed as
System.in
- Standard out (stdout), which normally goes to the console. Exposed as
System.out
- Standard error (stderr), which normally also goes to the console. Exposed as
System.err
- 标准输入 (stdin),通常来自键盘。暴露为
System.in
- 标准输出 (stdout),通常进入控制台。暴露为
System.out
- 标准错误 (stderr),通常也会进入控制台。暴露为
System.err
Your program is correct – it does print to stderr. But under normal circumstances, the stderr stream goes to the console just like the stdout stream, so they are visually indistinguishable.
你的程序是正确的——它确实打印到 stderr。但是在正常情况下,stderr 流和 stdout 流一样进入控制台,因此它们在视觉上无法区分。
However, the reason you should use stderr instead of stdout for error messages, is redirection. That means that you send stderr to a file instead of the console. Meanwhile, stdout will be unaffected, because the two streams are independent.
但是,您应该对错误消息使用 stderr 而不是 stdout 的原因是redirection。这意味着您将 stderr 发送到文件而不是控制台。同时,stdout 将不受影响,因为这两个流是独立的。
For example, you can do this in bash, cmd, PowerShell, etc:
例如,您可以在 bash、cmd、PowerShell 等中执行此操作:
$ java Program 2> errors.txt
Now, all output with System.err.println()
will end up in errors.txt
, while System.out.println()
will still go to the screen. This can help with debugging.
现在,所有输出 withSystem.err.println()
都将以 结束errors.txt
,而System.out.println()
仍会显示在屏幕上。这有助于调试。
回答by DopeGhoti
There are three data streams associated with nearly every process:
几乎每个进程都关联着三个数据流:
- Standard Input: This is the stream of input into a program, either from a terminal, a console, piped output from another process, or some other means.
- Standard Error: This is where all debugging and error messages should go. This is so that this sort of information can easily be separately captured from the regular output of a program. Web servers do this, by sending error messages to an
error_log
file viastderr
, while the normal log file would be e. g.access_log
. - Standard Output: This is the where all typical, expected output that a user running a program should expect to see said output appear.
- 标准输入:这是输入到程序的流,可以来自终端、控制台、来自另一个进程的管道输出或其他方式。
- 标准错误:这是所有调试和错误消息应该去的地方。这样可以轻松地从程序的常规输出中单独捕获此类信息。Web 服务器通过通过 将错误消息发送到
error_log
文件来执行此操作stderr
,而正常的日志文件将是例如access_log
. - 标准输出:这是运行程序的用户应该看到的所有典型的预期输出出现的地方。
Standard Output (stdout
) and Standard Error (stderr
) are nearly always the first and second output streams coming from a process, respectively. This allows me to do something like /path/to/my/neat/program > logs/program.log 2> logs/program.err
and have output and errors nicely sorted.
标准输出 ( stdout
) 和标准错误 ( stderr
) 几乎总是分别来自进程的第一个和第二个输出流。这让我可以做一些类似的事情,/path/to/my/neat/program > logs/program.log 2> logs/program.err
并对输出和错误进行很好的排序。