简体   繁体   中英

Use of System.err.println() in Java

On standard console all things are printed in white whether we have written it in System.out or System.err . In IDE(for me Eclipse) we can see different color output on console for both. ie black for System.out and red for System.err .

Is System.err is only provided for use in IDEs? Cause on cmd we can not distinguish System.out and System.err . Both are printed in same color.

These are two different output streams that are available in most of OS's. You don't have them color coded due to settings of your terminal/command line environment. On the other hand your IDE provides different visualization for different streams.

If you wanted to color them, consider using ANSI escape sequences.

System.out goes to the standard output stream (stdout) and System.err goes to the standard error stream (stderr). See standard streams for details and how you can control where they go. Eclipse just conveniently colour codes them for you so you can distinguish them in one view.

From system-in-out-error :

System.err is a PrintStream. System.err works like System.out except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text.

From JLS :

20.18.3 public static PrintStream err;

The initial value of this variable is a "standard" error output stream, already open and ready to accept output data. Typically, this corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored. Note that this field is not final, so its value may be updated if necessary.

This is a relict from the unix world, where most functionality is available as unix commands which were intended to be chained. The output of one command is used to feed another like here:

grep -i 'token' file | mail peter@address.de

The pipe symbol only redirects the stdout (System.out), but not the stderr (System.err). So error messages would be seen on the console, and the regular output would go to the mail command.

If there were just one stream, one could not distinguish between them.

Windows, not relying on the command line (This changed in Windows Server 2008!) didn't invent again but just took the unix concepts and made them available in their dos commands, too. It is just that nearly no Windows only users usually know what they are good for.

Example use:

try {
    Class.doSomething(myFile);
  } catch (Exception e){
    System.err.println("Fatal error performing doSomething: " + e);
    System.exit(-1);
  }

Both System.out and System.err always exist in Java.

Depending on your console it might be possible to get it to display the two streams in a different colour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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