In Java,
System.out print messages to the standard out of the system you are using,
where as System.err print messages to the standard error of the system. When
you run your Java program from command prompt/terminal, by default all the
print statements of System.out, System.err are print to system console (command
prompt/terminal).
For Example,
public class Test { public static void main(String args[]) { System.out.println("Information message 1"); System.err.println("error message 1"); System.out.println("Information message 2"); System.err.println("error message 2"); } }
Output
Information message 1 error message 1 Information message 2 error message 2
If you run
the same application in any IDE like Eclipse, System.err statements are
displayed in red color, where as System.out statements are displayed in black
color.
Redirecting error messages from command prompt
Try to
redirect the output of Test class to some file 'myLog'.
$ java Test > myLog error message 1 error message 2 $ $ $ cat myLog Information message 1 Information message 2
As you
observe, error messages are redirected to console, where as System.out messages
are redirected to myLog file. It is because, When you redirect console output
using the ">" symbol, you are only redirecting STDOUT. In order to
redirect STDERR you have to specify "2>" for the redirection
symbol.
$ java Test > mylog.out 2>mylog.err $ $ cat mylog.out Information message 1 Information message 2 $ $ cat mylog.err error message 1 error message 2
java Test > mylog.out 2>mylog.err
Above statement
redirects all the System.out messages to mylog.out file and all error messages
to mylog.err file.
No comments:
Post a Comment