2

Sorry if this is a nooby question.

When using System.out.println() followed by printing a stack trace, the two seem to overlap and interfere with eachother.

This code:

System.out.println("Multiple definitions for " + analyzer.name + ":");
for (String name : resultNames) {
    System.out.println('\t' + name);
}
throw new RuntimeException("Multiple class definitions found matching " + analyzer.name);

produces this output

Exception in thread "main" java.lang.RuntimeException: Multiple class definitions found matching RenderableNode
Multiple definitions for RenderableNode:
    at org.lime.apollo.updater.UpdaterApplication.runAnalyzer(UpdaterApplication.java:63)
    Analyzer1
    Analyzer2
    at org.lime.apollo.updater.UpdaterApplication.runAnalyzers(UpdaterApplication.java:70)
    Analyzer3
    at org.lime.apollo.updater.UpdaterApplication.main(UpdaterApplication.java:87)

The expected behavior is for Analyzer1, Analyzer2, etc to be printed before any of the stack trace. But instead the two seem to be printing on top of eachother, which makes a result that is hard to read.

What am I doing wrong?

Edit: Kon's answer below explains this, but I don't know how to mark as solved.

2
  • 6
    They are printing to different output streams (stderr and stdout) which are handled by different threads of the OS, I believe. That's why you see overlap. Commented May 15, 2015 at 21:47
  • Thank you! I replaced System.out.println with System.err.println and now I get what I expected. Commented May 15, 2015 at 21:49

1 Answer 1

2

System.out.println is buffered whereas error output is not buffered. That means that there is occasionally a delay before you see the results from System.out.println. Exceptions are reported through System.err.println, which is not buffered and so prints immediately.

Try switching your System.out.println statements to System.err.println, and see if the problem resolves.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.