aspose file tools*
The moose likes Beginning Java and the fly likes Difference between Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between "out" and "err"" Watch "Difference between "out" and "err"" New topic
Author

Difference between "out" and "err"

Saurabh Saha
Ranch Hand

Joined: Dec 08, 2004
Posts: 52
Hi All!
Please tell me what is the difference between out and err object. I know that out use for OutputStream and err use for Error. we use err object in the exception. but i'm not sure where to use err because most of the time we use out object. They give the same output like-

System.out.println("Hello");
and
System.err.println("Hello");

Please clear my doubt.

Thanks & Regards,
Vivek Mishra
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
By convention, the System.err used to display error message.


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

In most cases, it makes little difference. I have rarely seen applications that route standard out and err to different locations. Maybe it is because it is hard to debug an error, without the output of the program.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The main issue is that System.out may be redirected at either the Java or the operating system level to another device, while you still want to see the error mssages on the console. Or the reverse, if you want to log all error messages to disk.


Mike Gershman
SCJP 1.4, SCWCD in process
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

A bigger issue is that the standard-issue Windows command shell historically didn't, and perhaps still doesn't, let you redirect stderr to a file, making programs that use system.err for logging painful to work with.


[Jess in Action][AskingGoodQuestions]
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
I seem to recall that another difference is in the timing in which they are executed. For instance when I was learning threads I think they suggested using err because it was more reliable in terms of the timing of when it is written to the console. It's been a while and I could be wrong but I think that was the case.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Hentay Duke:
I seem to recall that another difference is in the timing in which they are executed.


It's indeed true that the err is often unbuffered (i.e., output appears immediately) and out is often buffered (i.e., output appears after newline, or call to flush().) But I believe this is an unspecified platform detail. I'm not 100% certain of that.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I also heard about the timing difference but, checking the source of java.lang.System, I see they now both call flush() after every println() in the Wintel version. This isn't covered in any doc I could find.
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
Looking at the Java 1.4 API, the only difference seems to be that (without using the set methods to redirect either) out prints to stdout (standard out) and err prints to stderr (standard error). On most systems, this will be the same thing most of the time. On my Linux box, I have tty10 setup to print out all stderr messages, and some logging applications may use stderr. One use I think is that you can redirect stderr to an error log, and then anytime the developer used System.err.print(ln) to indicate an error, it will show up in the logfile. Could somebody point me to where I can find information on wether out or err is flushed and when?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You can see in the source code for java.lang.System that both err and out are flushed after every println()
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
Hi,

General Idea ::
---------------

Probably the most often used items from the System class are the the standard output and standard error streams, which you use to display text to the user. The standard output stream is typically used for command output, to display the results of a command to the user. The standard error stream is typically used to display any errors that occur when a program is running.

Explaination ::
---------------
System.out leads the output to the standard output stream
(normally mapped to your console screen), System.err leads the
output to the standard error stream (by default the console, too).
The standard output should be used for regular program output,
the standard error for errormessages. If you start your console
program regularly both message types will appear on your screen.

But you may redirect both streams to different destinations (e.g. files),
e.g. if you want to create an error log file where you don't want to be
the regualr output in.

On an UNIX you may redirect the output as follows:

java yourprog.class >output.log 2>error.log

this causes your regular output (using System.out) to be stored in
output.log and your error messages (using System.err) to be stored
in error.log

System.err is a "special" pipe that usually is directed
to the standard consolle. You can redirect the System.out with the
normal pipe control (| or > , but System.err no. If you want to put
both the "normal" output and the "error" output to a file you must
use the special redirect 2>.

This allow you to send normal messages into a file or in the /null
black hole, but still receive the error messages on the console.

Thanxs
Sheetal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between "out" and "err"