views:

193

answers:

6

What are some scenarios in which java's System.out.println would fail to produce any output. I have a call to it inside of a method and sometimes when the method is called I get the println and othertimes I don't.

Update: I am also using System.out.flush() after the println.

Update: Thank you for the debugging help. It turned out a blocking call to open a dialog made output appear vastly out of the proper order. I thought the method I was trying to print messages for was being called when the dialog closed but the method itself was what was calling the dialog and so after the closing it was already past the printouts which was where i started looking for the test. If someone has the ability to delete this question as the issue was not what was originally asked it'd be appreciated.

+3  A: 

I have never seen this scenario before. In theory, it would only "fail" when the output isn't there where you expect it is. The output target can namely be changed using System#setOut().

BalusC
True. When I am unlucky enough to have to work with hideous libraries that just print random stuff on the stdout or stderr, with no way to turn them off, I am tempted to do System.setOut(new PrintStream(new NullOutputStream())); (http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/NullOutputStream.html), resetting stdout later. Nice work around for quick and dirty single threaded programs. But if this is the case and some library is doing this, it's even worse.
Dimitris Andreou
One may just print System.err.println(System.out);, to see if the stdout is changed since the start of the application. (If stderr itself is changed, this has to be printed elsewhere of course).
Dimitris Andreou
+3  A: 

System.out.println on some platforms uses buffered output. Depending on what your code is doing, it is possible that the buffers are not flushed before your program exits. Try putting System.out.flush() after your printlns and see if that helps.

Edit:

sometimes when the method is called I get the println and othertimes I don't

How are you verifying that the method is called but the println produces no output? Is it possible your method is throwing an Exception (which is then swallowed) before it gets to the println?

It would, of course, be very helpful to see some actual code.

Jason Day
[code] System.out.flush(); System.out.println("------------------------"); System.out.println("\n\n::LAT:: <methodname>("+status+")\n"); System.out.println("------------------------"); System.out.flush(); Thread.dumpStack(); [/code] at the top of the method. I know the method is being called because methods it invokes are successfully printing and their dumpStacks() show this method in the trace
lathomas64
Are those other methods *only* invoked by this method? Are there multiple threads? Thread.dumpStack prints to System.err, not System.out, so unless you are flushing System.err the stack dumps might not be in the order you are expecting.
Jason Day
+1  A: 

Where are you checking for your output? It's possible that System.out has been redirected elsewhere, so maybe you're looking in the wrong place.

casablanca
A: 

System.out.println is buffered output, if you do not flush the buffer, it may seem to 'wait' until the end of program. Sometimes, the program can die before flushing its buffers. System.out.flush() will force output to be flushed.

Elf King
A: 

It's possible that the file handle has been changed. I.e., stdout's file descriptor is no longer 1. I've seen this done in logging utilities where people don't want to go and catch any text that might be printed to a file descriptor, so instead they just redirect the stream to a file handle of an open file.

Here's an example in python:

import sys

h = open(r"C:\foo.txt","a+")

sys.stdout = h
sys.stdout.write("hey fellas")

h.close()

Run this at the cmdline, and you'll not get "hey fellas" printed out as you expect. Instead, it will be redirected to the file C:\foo.txt

Chris
+1  A: 

answer as per @BalusC's suggestion--

Thank you for the debugging help. It turned out a blocking call to open a dialog made output appear vastly out of the proper order. I thought the method I was trying to print messages for was being called when the dialog closed but the method itself was what was calling the dialog and so after the closing it was already past the printouts which was where i started looking for the test. If someone has the ability to delete this question as the issue was not what was originally asked it'd be appreciated.

lathomas64