File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stumbled across something strange today Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stumbled across something strange today" Watch "Stumbled across something strange today" New topic

Stumbled across something strange today

Danjel Nyberg

Joined: Sep 12, 2012
Posts: 15

I was just tweaking a bit and stumbled across something strange, which i can't explain

given this simple class:

this gives different output every time i run it...

expected output is:

Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(

but if I run it several times it could give me this output:

1Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(


My only explenation to this is that the update of output to the Eclipse console is not keeping up.

Any ideas or input?

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

It gives me all time first output.Although my thought about your second output, i don't care, it isn't topic of research.

Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

It's because the System.out.println() invocations go to the standard output stream (System.out) and the stack trace goes to the standard error stream (System.err). Two different buffered streams, which may or may not get flushed in the same order you invoke them. A simple way to make sure the output is always ordered the way you expect, in a single-threaded application, is to assign the same output stream to both System.out and System.err, using the System.setOut() and System.setErr() methods.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
I agree. Here's the link:
subject: Stumbled across something strange today
jQuery in Action, 3rd edition