Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ Idea debug/run console; System.out.flush not flushing

This has been bugging me for the last couple days, because it used to work. I upgraded my intellij and now it doesn't work. I don't want to go back, but I need an answer.

So, I'm writing a console application, and while it runs, I want to have a shell'd out display of progress. It works fine when it's running, but when I'm debugging in IntelliJ Idea, the System.out.flush() won't flush to the console unless the buffer contains a newline. I wrote the following unit test to find out if it was me or my application.

@Test
public void flushTest() {
    int loops = 100;
    for (int i = 0 ; i < 100 ; ++i) {
        System.out.print("This is a print, does it flush : " + i + "\r");
        if (i %10 == 0) {
            System.out.print("\n");
        }
        System.out.flush();
    }
}

Spoiler, it is the application. I don't know what's changed, but it makes it very difficult to debug display issues without doing a full build and running on the command line. If someone can help me figure out why this changed and/or help me fix it so It'll flush properly, I'd be very grateful.

like image 988
The Masked Crusader Avatar asked Sep 30 '16 19:09

The Masked Crusader


People also ask

How do I clear my console in Intellij?

Press Ctrl+L in the built-in terminal in IDE. It clears the terminal.

How do I skip a debug line in Intellij?

Only thing you can do is you can do step over (F8)-which will trace line by line without going inside any function. One more thing is Step out(Shift+F8)- which will go to next debug point directly by executing in between lines in that single step. No. "Resume Program" (F9) will continue until the next breakpoint.


2 Answers

Tested on version 2016.3.5, the problem is still there.

The bug is caused by all characters after "\r" will be cleared in the console.

For example, using a string "Test\r" will be got "Test" written to console, after cursor is moved to the left by the "\r", all the characters "Test" is cleared. So you won't see anything.

The workaround is to move "\r" to the beginning of the string. For example, "\rTest" will see the output in console.

The following codes will illustrate the bug. You will see "0000" and "111" will appear for 1 second and then all are cleared by "22".

System.out.print("0000\r");
Thread.sleep(1000);
System.out.print("111\r");
Thread.sleep(1000);
System.out.print("22\r");
like image 191
eos1d3 Avatar answered Sep 23 '22 07:09

eos1d3


Until this issue is not resolved, current workaround is to use -Deditable.java.test.console=true (specify it in idea.properties).

like image 39
Stanislav Poslavsky Avatar answered Sep 22 '22 07:09

Stanislav Poslavsky