Backend Development 7 min read

Using Jansi for Colored Console Output in Java: Installation, Demo, and Custom Wrapper Methods

This article introduces the Jansi library for Java, shows how to add it via Gradle or Maven, provides runnable demo code for printing colored text, explains the underlying builder field, lists supported ANSI colors, and offers simple wrapper utilities and a random‑color string extension.

FunTester
FunTester
FunTester
Using Jansi for Colored Console Output in Java: Installation, Demo, and Custom Wrapper Methods

While exploring the source code of another framework, I discovered that its console output includes colors, which led me to investigate Jansi. Jansi is a small ASL 2.0‑licensed Java library that enables the use of ANSI escape sequences to format console output.

Dependencies

Gradle

// https://mvnrepository.com/artifact/org.fusesource.jansi/jansi
implementation group: 'org.fusesource.jansi', name: 'jansi', version: '2.4.0'

Maven

org.fusesource.jansi
jansi
2.4.0

Demo

The official documentation provides only API docs, so here are a few working examples:

import com.funtester.frame.SourceCode;
import org.fusesource.jansi.Ansi;

public class Jnst extends SourceCode {

    public static void main(String[] args) {
        String blue = Ansi.ansi().fg(Ansi.Color.BLUE).a("蓝色FunTester").reset().toString();
        output(blue);
        String red = Ansi.ansi().fgRed().a("红色FunTester").reset().toString();
        output(red);
        String red1 = Ansi.ansi().fgBrightRed().a("红色FunTester").fgYellow().a("黄色FunTester").fgBrightGreen().a("绿色FunTester").reset().toString();
        output(red1);
        Ansi ansi = Ansi.ansi();
        Ansi a = ansi.fgRed().a("红色FunTester").fgBrightGreen().a("绿色FunTester");
        output(ansi);
        output("会打印绿色字符");
        ansi.reset();
        output(a);
        output("会打印正常颜色字符");
    }
}

Each Ansi object contains a private final StringBuilder builder field; resetting this builder (e.g., via reflection) allows reuse of the same Ansi instance.

Supported Colors

BLACK(0, "BLACK"),
RED(1, "RED"),
GREEN(2, "GREEN"),
YELLOW(3, "YELLOW"),
BLUE(4, "BLUE"),
MAGENTA(5, "MAGENTA"),
CYAN(6, "CYAN"),
WHITE(7, "WHITE"),
DEFAULT(9, "DEFAULT");

Wrapper Utilities

The following simple wrapper methods reduce boilerplate when printing colored text. The reflection‑based reuse approach was abandoned due to performance concerns.

public static String color(Ansi.Color color, Object o) {
    return ansi().fg(color).a(o.toString()).reset().toString();
}

public static void black(String str) {
    System.out.println(ansi().fgBlack().a(str));
}

public static void blue(Object o) {
    output(color(Ansi.Color.BLUE, o));
}

public static void red(Object o) {
    output(color(Ansi.Color.RED, o));
}

public static void green(Object o) {
    output(color(Ansi.Color.GREEN, o));
}

public static void yellow(Object o) {
    output(color(Ansi.Color.YELLOW, o));
}

public static void white(Object o) {
    output(color(Ansi.Color.WHITE, o));
}

public static void black(Object o) {
    output(color(Ansi.Color.BLACK, o));
}

public static void gyan(Object o) {
    output(color(Ansi.Color.CYAN, o));
}

public static void magenta(Object o) {
    output(color(Ansi.Color.MAGENTA, o));
}

Extension: Random Color per Character

A method that assigns a random non‑black color to each character of a string (useful when the console background is black):

public static String rgb(String str) {
    char[] array = str.toCharArray();
    List
collect = Stream.of(Ansi.Color.values())
        .filter(v -> v != Ansi.Color.BLACK)
        .collect(Collectors.toList());
    Ansi ansi = ansi();
    for (int i = 0; i < array.length; i++) {
        ansi.fg(random(collect)).a(array[i]);
    }
    return ansi.reset().toString();
}

The same logic can be compressed into a single line using streams and lambdas:

public static String rgb(String str) {
    return Stream.of(ArrayUtils.toObject(str.toCharArray()))
        .map(f -> color(random(Stream.of(Ansi.Color.values())
            .filter(v -> v != Ansi.Color.BLACK)
            .collect(Collectors.toList())), f))
        .collect(Collectors.joining());
}

These examples demonstrate how Jansi can be leveraged to produce colorful console output with minimal code.

Follow FunTester for more tips and tricks!

javaGradleMavencode examplesANSI colorsconsole outputJansi
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.