Java: synchronizing standard out and standard error

后端 未结 6 1180
孤城傲影
孤城傲影 2020-12-03 18:38

I have a strange problem and it would be nice if I could solve it. For the debugging purposes (and some other things, as well) I\'m writing a log of a console Java applicati

6条回答
  •  难免孤独
    2020-12-03 19:00

    The problem is that it's the responsibility of the terminal emulator (in your case, Eclipse) to process the standard output and the standard error of your application. Without communicating with the terminal emulator, you can never be sure that out and err are displayed in the right order. Therefore, I would consider printing everything on err and redirect it to a file. You can still use out for clean user interaction.

    Nevertheless, there is a (very bad, but strict) solution to your problem:

    System.out.println(...);
    System.out.flush();
    Thread.sleep(100);
    
    System.err.println(...);
    System.err.flush();
    Thread.sleep(100);
    

    You may have to change the sleep duration depending on your configuration!

提交回复
热议问题