System.out.println not functioning

邮差的信 提交于 2019-12-01 19:49:01

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.

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

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!