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.
Press Ctrl+L in the built-in terminal in IDE. It clears the terminal.
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.
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");
Until this issue is not resolved, current workaround is to use
-Deditable.java.test.console=true
(specify it in idea.properties
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With