4

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.

lathomas64
  • 1,612
  • 5
  • 21
  • 47
  • 1
    Are you sure the execution is reaching the call to println? Have you hooked it up to a debugger to ensure that it is being called? – Fostah Jul 21 '10 at 17:29
  • Are you sure the method is being called? – Freiheit Jul 21 '10 at 17:30
  • 1
    By failing to write output, do you mean to say that the call is failing to write to the console? In such a case, check if the 'out' stream has been redirected. – Vineet Reynolds Jul 21 '10 at 17:32
  • 4
    Just put a println before to make sure it's being cal...oh, wait... – Justin Ardini Jul 21 '10 at 17:33
  • 2
    You may want to provide a bit more context into your question. E.g. some more detail about the environment and the code used and whether you was able to reproduce this in different environment and/or with some simple "hello world" code. – BalusC Jul 21 '10 at 17:40
  • @lathomas: as per your update, you should post it as an answer and then mark it accepted (whenever possible, if I recall correctly, you can accept own answers only after 24 hours). – BalusC Jul 21 '10 at 22:12

6 Answers6

6

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
  • 8,809
  • 1
  • 41
  • 46
  • [code] System.out.flush(); System.out.println("------------------------"); System.out.println("\n\n::LAT:: ("+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 Jul 21 '10 at 18:05
  • 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 Jul 21 '10 at 20:11
4

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 Jul 21 '10 at 17:44
  • 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 Jul 21 '10 at 17:44
2

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
  • 69,683
  • 7
  • 133
  • 150
1

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 passed the printouts which were 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
  • 1,612
  • 5
  • 21
  • 47
0

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
  • 1,189
  • 5
  • 7
0

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,421
  • 3
  • 18
  • 31