Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.println not functioning

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.

like image 954
lathomas64 Avatar asked Dec 17 '22 23:12

lathomas64


2 Answers

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.

like image 82
Jason Day Avatar answered Dec 19 '22 13:12

Jason Day


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().

like image 21
BalusC Avatar answered Dec 19 '22 14:12

BalusC