I'm trying to print colored text to the console on Windows, and it requires the use of an escape character \u001B
.
In my IntelliJ IDEA the code System.out.print("\u001B[92m Hello!"):
prints green text " Hello!".
Unfortunately it doesn't work for the console if I run my application from CMD. I tried to use ASCII and UTF-8 encoding but it doesn't help.
For example aforementioned and following snippets
try (OutputStreamWriter writer = new OutputStreamWriter(
System.out,
StandardCharsets.UTF_8))
{
writer.write("\u001B[92m Hello!");
}
prints something like this <question mark in rectangle>[92m Hello!
Info: For the last example I changed the console codepage to Utf-8 with command chcp 65001 and I also run my java app with -Dfile.encoding=UTF-8
flag enabled so Java and the console use same encoding. Result is the same.
It is interesting, because I can print colored text from .bat scripts. For example the task can be done by echo <ESC>[92m Hello!
where <ESC>
is an escape character that can be typed to Notepad++ with keyboard combo Alt + 0 + 2 + 7.
More interesting thing is that Java correctly outputs escape character <ESC>
to external file. I would be glad to know of working (preferably native) solutions to this problem.