简体   繁体   English

System.out.println无法正常运行

[英]System.out.println not functioning

What are some scenarios in which java's System.out.println would fail to produce any output. 在什么情况下,java的System.out.println将无法生成任何输出。 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. 我在一个方法内部调用它,有时当调用该方法时,我得到println,而不是我得到的othertimes。

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. 如果某人有能力删除此问题,因为问题不是最初提出的问题,我们将不胜感激。

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()进行更改。

System.out.println on some platforms uses buffered output. 某些平台上的System.out.println使用缓冲输出。 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. 尝试在println之后放入System.out.flush()并查看是否有帮助。

Edit: 编辑:

sometimes when the method is called I get the println and othertimes I don't 有时当调用该方法时,我会得到println,而不是我得到的othertimes

How are you verifying that the method is called but the println produces no output? 你如何验证方法被调用但println没有产生输出? Is it possible your method is throwing an Exception (which is then swallowed) before it gets to the println? 您的方法是否有可能在它到达println之前抛出异常(然后被吞下)?

It would, of course, be very helpful to see some actual code. 当然,看到一些实际的代码会非常有帮助。

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可能已被重定向到其他地方,所以也许你正在寻找错误的地方。

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. 如果某人有能力删除此问题,因为问题不是最初提出的问题,我们将不胜感激。

System.out.println is buffered output, if you do not flush the buffer, it may seem to 'wait' until the end of program. System.out.println是缓冲输出,如果你不刷新缓冲区,它可能似乎“等待”直到程序结束。 Sometimes, the program can die before flushing its buffers. 有时,程序可能会在刷新缓冲区之前死亡。 System.out.flush() will force output to be flushed. System.out.flush()将强制刷新输出。

It's possible that the file handle has been changed. 文件句柄可能已更改。 Ie, stdout 's file descriptor is no longer 1 . 即, stdout的文件描述符不再是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: 这是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. 在cmdline上运行它,你不会像你期望的那样打印出“嘿fellas”。 Instead, it will be redirected to the file C:\\foo.txt 相反,它将被重定向到文件C:\\ foo.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM