A splash of text color with your Java

Get more control over Java console

December 20, 2002

Q: I want to use colors and fonts in Java console. Is that possible?

A: As the simplest option, if your target platform and shell support ANSI escape sequences, you might try sending them to System.out. For instance, ESC [2J is an escape sequence for VT100-compatible terminals and Windows ANSI.SYS drivers that means "erase the entire display":

    System.out.println ((char)27 + "[2J");

However, escape sequences are not very portable. For example, Windows NT provides an ANSI.SYS driver, but only for legacy DOS applications (which a JVM is not). I do not recommend this option except for the most basic functionality for a chosen OS/shell combination.

A typical Unix approach for building text GUIs is to use a higher level library such as curses. There are various Java curses ports available on the Web. They must employ native code via Java Native Interface (JNI), which means your application will only be as portable as your willingness to maintain and deploy these platform-specific libraries.

For a concrete example, Alexei Chmelev's Java Curses Library (JCurses) offers a JNI-based implementation that uses curses on Unix and Win32 to provide a platform-independent text windowing toolkit. The following JCurses program creates a 40x20 window in the center of your console and prints "Hello World!" in it:

import jcurses.system.CharColor;
import jcurses.widgets.*;
public class Main
    public static void main(String[] args) throws Exception
        Window w = new Window(40, 20, true, "Hello World Window");
        DefaultLayoutManager mgr = new DefaultLayoutManager();
            new Label("Hello World!", new CharColor(CharColor.WHITE, CharColor.GREEN)),
            0, 0, 40, 20,
        w.close(); // reset the native console

A completely different approach is to create a console look-alike in AWT or Swing, add an API for managing fonts and colors, and couple it with a PrintStream override that can be plugged in as System.out. This is precisely what Ethan Nicholas's Enigma Console does. Here is the Enigma Console version of "Hello World":

import java.awt.Color;
import enigma.console.*;
import enigma.core.Enigma;
public class Main
    public static void main(String[] args)
        TextAttributes attrs = new TextAttributes(Color.BLUE, Color.WHITE);
        System.out.println("Hello World!");
    private static final Console s_console;
        s_console = Enigma.getConsole("Hellow World!");

Using Enigma.getConsole() above has the side effect of installing Enigma Console as a System.out replacement. This makes it fairly easy to retrofit Enigma Console into an existing Java application.

Vladimir Roubtsov has programmed in a variety of languages for more than 12 years, including Java since 1995. Currently, he develops enterprise software as a senior developer for Trilogy in Austin, Texas.

Learn more about this topic