Skip to content

Migrating from jansi to jline jansi makes colors not working anymore in a uber jar #1292

@AlexisJehan

Description

@AlexisJehan

OS: Windows 11
Java distribution: Adoptium 11 / Adoptium 24

Hello,
Consider a simple application that use Jansi to print a message in red, packaged as an executable uber JAR:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>foo</groupId>
	<artifactId>foo-jansi</artifactId>
	<version>1.0.0-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.release>11</maven.compiler.release>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.fusesource.jansi</groupId>
			<artifactId>jansi</artifactId>
			<version>2.4.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.7.1</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>foo.Application</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>assembly-single</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
package foo;

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;

public final class Application {

	private Application() {}

	public static void main(final String... args) {
		AnsiConsole.systemInstall();
		Ansi.setEnabled(true);
		final var out = AnsiConsole.out();
		out.println(Ansi.ansi().fgBrightRed().a("foo").reset());
		AnsiConsole.systemUninstall();
	}
}

The output is working as expected on Windows Terminal:
java -jar foo-jansi-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Image

Now, let's migrate this application from the old Jansi project to JLine, following the note from the last Jansi release: https://github.com/fusesource/jansi/releases/tag/jansi-2.4.2

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>foo</groupId>
	<artifactId>foo-jline</artifactId>
	<version>1.0.0-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.release>11</maven.compiler.release>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.jline</groupId>
			<artifactId>jansi</artifactId>
			<version>3.30.3</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.7.1</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>foo.Application</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>assembly-single</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
package foo;

import org.jline.jansi.Ansi;
import org.jline.jansi.AnsiConsole;

public final class Application {

	private Application() {}

	public static void main(final String... args) {
		AnsiConsole.systemInstall();
		Ansi.setEnabled(true);
		final var out = AnsiConsole.out();
		out.println(Ansi.ansi().fgBrightRed().a("foo").reset());
		AnsiConsole.systemUninstall();
	}
}

This time with the exact same code using JLine instead of Jansi, the output is not working anymore on Windows Terminal:
java -jar foo-jline-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Image

Any idea why and how to fix it?

foo-jansi.zip

foo-jline.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions