Java System.out.println 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3302060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.out.println not functioning
提问by lathomas64
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.
在哪些情况下,java 的 System.out.println 将无法产生任何输出。我在一个方法内部调用了它,有时在调用该方法时我得到了 println ,而有时我没有。
Update: I am also using System.out.flush() after the println.
更新:我也在 println 之后使用 System.out.flush()。
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.
更新:感谢您的调试帮助。结果发现,打开对话框的阻塞调用使输出显示的顺序大大不正确。我以为我试图打印消息的方法在对话框关闭时被调用,但方法本身就是调用对话框的方法,所以在关闭之后它已经超过了打印输出,这是我开始寻找测试的地方。如果有人能够删除此问题,因为该问题不是最初提出的问题,我们将不胜感激。
采纳答案by lathomas64
answer as per @BalusC's suggestion--
根据@BalusC 的建议回答--
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.
感谢您的调试帮助。结果发现,打开对话框的阻塞调用使输出显示的顺序大大不正确。我认为我试图打印消息的方法在对话框关闭时被调用,但方法本身就是调用对话框的方法,因此在关闭后它已经通过了打印输出,这是我开始寻找测试的地方。如果有人有能力删除此问题,因为问题不是最初提出的问题,我们将不胜感激。
回答by BalusC
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()
.
我以前从未见过这种场景。理论上,只有当输出不在您期望的位置时它才会“失败”。可以使用 更改输出目标System#setOut()
。
回答by Jason Day
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 println
s and see if that helps.
System.out.println
在某些平台上使用缓冲输出。根据您的代码在做什么,在您的程序退出之前缓冲区可能没有被刷新。试着把System.out.flush()
你的println
s放在后面,看看是否有帮助。
Edit:
编辑:
sometimes when the method is called I get the println and othertimes I don't
有时当方法被调用时我得到 println 有时我没有
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?
您如何验证该方法已被调用但 println 不产生任何输出?您的方法是否有可能在到达 println 之前抛出异常(然后被吞下)?
It would, of course, be very helpful to see some actual code.
当然,查看一些实际代码会很有帮助。
回答by casablanca
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.
你在哪里检查你的输出?可能System.out
已被重定向到其他地方,所以您可能找错了地方。
回答by Elf King
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.
System.out.println 是缓冲输出,如果您不刷新缓冲区,它可能会“等待”直到程序结束。有时,程序可能会在刷新其缓冲区之前终止。System.out.flush() 将强制刷新输出。
回答by Chris
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.
文件句柄可能已更改。即,stdout
的文件描述符不再是1
。我已经在日志记录实用程序中看到了这一点,人们不想去捕获任何可能打印到文件描述符的文本,因此他们只是将流重定向到打开文件的文件句柄。
Here's an example in python:
这是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
在 cmdline 运行这个,你不会像你期望的那样打印出“嘿家伙”。相反,它将被重定向到文件 C:\foo.txt