Explain Codes LogoExplain Codes Logo

How to print color in console using System.out.println?

java
ansi-codes
console-colors
jcolor
Alex KataevbyAlex Kataev·Aug 7, 2024
TLDR

To create colorful console output in Java, you can utilize the power of ANSI escape codes:

final String ANSI_RED = "\u001B[31m"; final String ANSI_RESET = "\u001B[0m"; System.out.println(ANSI_RED + "This text is red." + ANSI_RESET);

This changes the text color to red using \u001B[31m, then it resets the color with \u001B[0m. Usually, terminals supporting ANSI colors will display this correctly, however, it's beneficial to verify compatibility.

A spectrum of opportunity with ANSI codes

Dynamically change your console's background and text color by defining color constants. This facilitates an easy switch between colors in your code, increasing its readability and maintainability.

final String ANSI_YELLOW = "\u001B[33m"; final String ANSI_RESET = "\u001B[0m"; System.out.println(ANSI_YELLOW + "Caution! Wet paint!" + ANSI_RESET);

Colors for friendly data differentiation

Use different ANSI colors for different data types to aid in the identification of errors, informational log, critical warning, or verbose log. With Windows environments, boost the support of ANSI codes using the Jansi library.

// This Gray color won't suck your soul :D final String ANSI_GRAY = "\u001B[1;30m"; ... System.out.println(ANSI_GRAY + "Verbose log: Loading user data..." + ANSI_RESET);

Mastering external libraries and utilities

COLORS powered by JColor

The JColor library facilitates manipulating console colors with its easy-to-use 8 and 24-bit color formats, allowing your console outputs to shine.

JColor.colorize("Hello, JColor!", Color.magenta);

Conquer complexity with ConsoleColors class

A custom ConsoleColors class simplifies color management by providing public static fields for each common color.

public class ConsoleColors { // Welcome to the rainbow factory 🌈 public static final String RED = "\033[0;31m"; public static final String GREEN = "\033[0;32m"; public static final String BLUE = "\033[0;34m"; public static final String RESET = "\033[0m"; }

Colorful enums for dynamic control

An enum "Color" with pre-defined escape sequences promotes dynamic color output while maintaining readability.

public enum Color { RED("\033[0;31m"), GREEN("\033[0;32m"), YELLOW("\033[0;33m"); private String code; Color(String code) { this.code = code; } public String getCode() { return code; } }

Caution: Older Windows consoles and ANSI

Older Windows consoles may struggle with ANSI escape codes. Check your audience's terminal to ensure a seamless user experience.

Verifying ANSI compatibility

Before painting with all the colors of the wind, ensure the terminal supports ANSI escape codes.

Friendly fallback

In unsupported terminals, printing the message without ANSI codes maintains your application's professional demeanor and coherence.

if(!terminalSupportsAnsi()) { // If only every fallback could be this graceful 🕊️ System.out.println("Same flavor, less color!"); }

Never forget to hit Reset

Ensure to reset the console color after each output, to protect the succeeding text from unwanted coloring.

// It's not that I'm lazy, I just care about your eyes. 😉 System.out.println("\033[0m");