Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a blurb about it in DevX
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then how to direct output messages to a file & error messages to a different file.
Is there any standard way?
Regards.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check out the link BV Boose posted?
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah that shows how to do it in UNIX.
But I want it in Windows
Regards.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the "> /null" (which should be /dev/null), it will work just the same on Windows as well.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appending instead of overwriting can be done by using ">>" instead of ">".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic