• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah that shows how to do it in UNIX.
But I want it in Windows
Regards.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appending instead of overwriting can be done by using ">>" instead of ">".
 
reply
    Bookmark Topic Watch Topic
  • New Topic