Sitemap

Java; terminal with ANSI colors in Windows, Linux & IntelliJ

2 min readMar 30, 2025

--

Originally published on https://wasteofserver.com/java-terminal-with-ansi-colors-in-windows-linux-intellij/, you will find newer revisions and additional comments there.

I’ve run into this problem more times than I’d like, always having to retrace my steps to get things working again; so I’m putting this online as a reference.

If you’re here, you probably already know what ANSI escape sequences are.

You output something like the code below, and it prints in color:

System.out.println("world is black and white");
System.out.println("\u001B[34m" + "world has color" + "\u001B[0m");

You can check the rest of ANSI codes here

We don’t often manually write System.out directly to the console. More likely, you're using something like Logback to manage your output and have configured logback.xml for color output.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyymmdd HH:mm:ss.SSS} %highlight(%-5level) [%blue(%t)] %yellow(%C): %msg%n%throwable</pattern>
</encoder>
</appender>

<root level="trace">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>

This will give you color! Just not on Windows.

To get color to work on Windows, you need to install Jansi:

<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.1</version>
</dependency>

If you only install AnsiConsole without enabling pass-through, it will stop working in IntelliJ. To get it working on both Windows and IntelliJ, you need to do both; install AnsiConsole on startup and allow codes to pass through.

public static void main(String[] args) {
// for colors to work in Windows, linux and IntelliJ
System.setProperty("jansi.passthrough", "true");
AnsiConsole.systemInstall();

log.trace("trace");
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
}

Hope it helps. I, for sure, know that’ll I’ll be here in a couple of months, rechecking how to do this once again! ;)

Originally published at https://wasteofserver.com on March 30, 2025.

--

--

No responses yet