When running a Java application from Gradle via ‘cmd.exe’ or ‘console.exe’ terminal applications, some ‘System.out’ lines returned by the ‘JavaExec’ task will include additional white spaces making the output difficult to read. This is a Windows only problem. (Using terminal mintty instead doesn’t show the same problem but then all colored output and the prompt is gone.)
Nothing special to say about the task declaration …
Hi Perryn - Sorry for not being precise enough. Yes, the Simulator class prints to ‘System.out’ which in turn is mapped to the ‘JavaExec.standardOuput’. We don’t do anything fancy though. One of our commands is simply doing an ‘ls’ on the current directory. Usually the first two items are mangled, with more return results more mangled output happens in between. Thanks for checking…/Thomas
Correct, not when running the Java directly. Not when executing via Gradle from a Linux terminal. Not when executing via Gradle from the mintty terminal. Only when using native Windows terminal.
Well, I was hoping you have a test case for this anyway that could be used verify this using the environment specified. If not, I guess I could write a simple program that shows the problem.
OK, here is a simple sample representing the problem. I guess I should have provided this in the first place …
defaultTasks 'badconsole'
task badconsole(type: ConsoleTask)
class ConsoleTask extends DefaultTask {
@TaskAction def foo() {
String[] lines = ['one', 'two\nthree', 'four\nfive', 'six seven eight\n\nnine']
for (String line : lines)
System.out.print(line + '\n')
for (String line : lines)
System.out.println(line)
}
}
And here comes the output …
$ gradle
:badconsole
one
two
three
four
five
six seven eight
nine
one
two
three
four
five
six seven eight
nine
BUILD SUCCESSFUL
Total time: 3.008 secs
The output looks actually a little worse which seems to be hard to render in the HTML. But anyway, seems like it has problems with ‘print()’ when the input has embedded ‘\n’; whereas ‘println()’ seems to be OK.
Actually, while pasting in the sample code I noticed issues with it which I therefore edited. In the edited version it looks all correct. So let me get back to the original code and verify if this isn’t an issue with the usage of ‘print()’ statements there or come-up with a better sample.
$ gradle
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:badconsole
Just 10 lines of output ...
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Just 10 lines ouf output ...
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
BUILD SUCCESSFUL
Total time: 3.281 secs
Actually, you have a good point here, replacing ‘\n’ by ‘\r\n’ makes the problem disappear. But why does this cause problems only for line 2 and not when executing the class directly from Java?