Pregunta

¿Cuáles son algunos escenarios en los que System.out.println de Java no produciría ningún resultado?Tengo una llamada dentro de un método y, a veces, cuando se llama al método, obtengo println y otras veces no.

Actualizar:También estoy usando System.out.flush() después de println.

Actualizar:Gracias por la ayuda de depuración.Resultó que una llamada de bloqueo para abrir un cuadro de diálogo hacía que la salida pareciera muy fuera del orden correcto.Pensé que el método para el que estaba intentando imprimir mensajes se llamaba cuando se cerraba el cuadro de diálogo, pero el método en sí era lo que llamaba al cuadro de diálogo y, por lo tanto, después del cierre ya había pasado las impresiones, que fue donde comencé a buscar la prueba.Si alguien tiene la posibilidad de eliminar esta pregunta ya que el problema no era el que se preguntó originalmente, se lo agradecería.

¿Fue útil?

Solución 4

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.

Otros consejos

System.out.println en algunas plataformas utiliza salida almacenada en búfer.Dependiendo de lo que esté haciendo su código, es posible que los buffers no se vacíen antes de que salga su programa.intenta poner System.out.flush() despues de ti printlns y ver si eso ayuda.

Editar:

A veces, cuando se llama al método, obtengo println y otras veces no.

¿Cómo se verifica que se llama al método pero println no produce ningún resultado?¿Es posible que su método arroje una excepción (que luego se traga) antes de llegar a println?

Por supuesto, sería muy útil ver algún código real.

Nunca he visto este escenario antes.En teoría, solo "fallaría" cuando la producción no está allí donde espera que sea.El objetivo de salida puede ser cambiado a través de 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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top