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

System.out v/s System.err

Kaushal Wadhwani
Greenhorn

Joined: Aug 22, 2009
Posts: 5
Consider the following program :


public class kaushal
{

public static void main(String[] args)
{

System.out.println("xyz");
System.err.println("xyz2");
System.err.println("xyz2");
System.err.println("xyz3");
System.out.println("xyz");
System.out.println("xyz");
System.err.println("xyz3");
}
}

As shown I have used err and out class and when I run this program in NetBeans a strange behaviour is seen :

The output is :

xyz
xyz2
xyz2
xyz3
xyz3
xyz
xyz

Sometimes it would come in proper sequence sometimes in odd.

Whereas if I use the console version no matter how many times I try the output is in proper sequence.

Can someone please explain this odd behaviour in NetBeans.

I am using Netbeans 6.7.1.
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
i ran it in Eclipse, here's the output:

xyz2
xyz2
xyz3
xyz3
xyz
xyz
xyz

hmm...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Kaushal,
Welcome to JavaRanch!

System.out and System.err are streams. They have the option of writing immediately or buffering and writing later. And even if they write immediately, you have a threading problem of how long it takes to be "immediate."

As a result, it's like having two people writing to the same place. You are guaranteed that each of the writers will have its output in the correct order, but not that they will between them.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
wow..., that's why you are a 'Sheriff'. could you please explain or give a link on the concept of a 'stream'. BTW what IDE do you use Jeanne?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
venu chakravorty wrote: . . . BTW what IDE do you use Jeanne?
If you are a real beginner, you shouldn't ask that sort of question. You should avoid IDEs until you are more experienced.

If you are more experienced, you should try different IDEs and see which you are happiest with. Some people like one, some people are happier with another.
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
thanks, i'll keep that in mind Campbell.
Kaushal Wadhwani
Greenhorn

Joined: Aug 22, 2009
Posts: 5
thanks sheriff.....i got dat....

can you please tell me how you get to know about this...i tried at many places but couldnt get a satisfactory answer regarding this...

wherever i read, this thing about streams was not written..i tried java.sun.com and other places like these
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
ok, 2 streams are opened and they try to write simultaneously... fine, but whatever happens should be same weather the program is run under an IDE or the terminal, so why do we see different outputs?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

venu chakravorty wrote:ok, 2 streams are opened and they try to write simultaneously... fine, but whatever happens should be same weather the program is run under an IDE or the terminal


Incorrect, applications are not guaranteed to behave the same under all environments. The JVM must implement the Java Specifications for the run-time environment, and the contracts created by the Java API, but nothing more, and those specs leave a lot out - for example they don't guarantee much of of anything about order of operations between threads, except where synchronized barriers are concerned.

An IDE may be using a different JVM than your terminal window, and if using the same one it could be using different run-time parameters. So there should be no expectation that the IDE behaves just like the terminal window.

, so why do we see different outputs?

Because you are running the application in 2 different and unrelated environments.


Steve
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Kaushal Wadhwani wrote:wherever i read, this thing about streams was not written..i tried java.sun.com and other places like these

You saw it written here . Seriously though - if you want something more authoritative, try the System JavaDoc. It shows that both System.out and System.err are of the PrintStream class.

venu chakravorty wrote:BTW what IDE do you use Jeanne?

For Java - Eclipse. But that doesn't mean anything. I also code in vi when shell scripting because it's what's available.
Murali Narayanaswamy
Greenhorn

Joined: May 29, 2007
Posts: 24
System.out messages can be written to a file where as System.err could not.
If you run the program like java kaushal ->kaushal .txt, you could see the System.out messages in kaushal .txt and System.err messages in the console.

Ta
Murali
Kaushal Wadhwani
Greenhorn

Joined: Aug 22, 2009
Posts: 5
I went on to the System JavaDoc link given by you Sheriff but what I was looking for was an official explanation about the behaviour of the classes regarding buffering and threading.

I just mean to ask, is this an assumption based on the output that we observe or is this a concrete explanation regarding System.out and System.err classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.out v/s System.err