Writing Colorful Console Output in Java

Almost all of the Java console application I had seen were boring black background and white fonts on Windows. This can be changed using a small Java library called Jansi which is open sourced under Apache 2.0. The download link can be found at its project home. The source code is also available on Github here.

The trick behind the library is the ANSI escape code standard. It basically defines a number of escape codes for terminal operations and changing the font colors. In some way, you can think it as html tags when it comes to color texts.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

Although the result looks great, I did find it’s difficult to use without a complete and working sample. It took me hours to get a good sample code snippet by huyunhjl who had cloned the Jansi library. After reading the short introduction, I found the library is actually quite easy to use.

To enable/disable the ANSI capability, you would need to install/uninstall it with a static method:


Besides the full API version for changing the color, there is an alternative way with mark up like @|red etc.

System.out.println( ansi().eraseScreen().render("@|red Hello|@ @|green World|@") );

The following is a quick HelloWorld example. Note: To see the result, you got to run it in a console, not in IDE which does allow you to print out messages.

import org.fusesource.jansi.AnsiConsole;
import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;
 * @author Steve Jin (http://www.doublecloud.org)
public class ShowColorText
  public static void main(String[] args)
    System.out.println(ansi().eraseScreen().fg(RED).a("Hello").fg(YELLOW).a(" World").reset());
    System.out.println( ansi().eraseScreen().render("@|red Hello|@ @|yellow World|@") );

If you use Maven, you can add the following dependency in your pom.xml file:

This entry was posted in Software Development and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.