This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Use of System.err instead of System.out in printing error messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of System.err instead of System.out in printing error messages" Watch "Use of System.err instead of System.out in printing error messages" New topic
Author

Use of System.err instead of System.out in printing error messages

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear all,
I want to know what is the use of System.err instead of System.out in printing error messages?
Any specific reason or just because it is a good practice we're using it?
Regards.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

a lot of the time, you want to separate your regular output from your error output.

Say your program is processing a million records, and generating output that will be fed to another program. some of the records will fail. You don't want 20k lines of good data, a bunch of error messages, then 30k more good data, then more error messages...

if you separate your standard out from your error out, you can generate a file with all the good data, and feed it straight into your next program. You can then check your error file for the records that failed, and decide how to handle those later.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
here's a blurb about it in DevX
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Then how to direct output messages to a file & error messages to a different file.
Is there any standard way?
Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Did you check out the link BV Boose posted?


Ping & DNS - my free Android networking tools app
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
yeah that shows how to do it in UNIX.
But I want it in Windows
Regards.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19677
    
  18

Except for the "> /null" (which should be /dev/null), it will work just the same on Windows as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Now it's working particularly fine.
public class Errorlog
{
public static void main(String[] args)
{
System.out.println("This is output file");
System.err.println("This is an errorlog file");
}
}




After compiling and running the class file by giving the following command ,
java Errorlog>output.log 2>error.log


I get the following message in output.log
This is output file


error.log:
This is an errorlog file


But usually when I change the output message & error message content, these messages often override the previous data in these files.

Consider now that I modify the class file as:
public class Errorlog
{
public static void main(String[] args)
{
System.out.println("New output");
System.err.println("New error ");
}
}



Now after compiling & running the class file by giving these command
java Errorlog>output.log 2>error.log


The content in output.log is:
New output

Clearly it had overridden the previous message:" This is output file"
Same is the case in error.log file also
I want the output.log to display:
This is output file
New output




How this can be done?
Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Appending instead of overwriting can be done by using ">>" instead of ">".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of System.err instead of System.out in printing error messages