9

I was wondering if there is someway for me to set the color of the text that I output to the console in Java. It does not matter if it is system specific as the program will only be run on my Windows 7 x64 laptop.

This question: Change color in java eclipse console was asked several weeks ago and had a good solution(by @VonC) to a similar problem however it only addressed the issue inside eclipse.

Can the same effect be achieved if I execute my program from the command line? and if so how?

Community
  • 1
  • 1
chandsie
  • 2,785
  • 3
  • 26
  • 36
  • 2
    possible duplicate of [How to print color in console using System.out.println?](http://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println) – Nateowami Jan 25 '15 at 13:11

4 Answers4

10

You can take a look at the Java Curses Library: http://sourceforge.net/projects/javacurses/

Here's an entry on how to use it: http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html

Kaj
  • 10,744
  • 1
  • 31
  • 27
8

thy this.... furthermore read http://jansi.fusesource.org/

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
a.s.p.
  • 318
  • 1
  • 3
  • 12
  • 2
    Don't forget to add usage: System.out.println( " This will be purple: " + PURPLE + "Purple!" + RESET + " normal color text" ); – will.fiset Dec 12 '14 at 05:25
7

Another library you may be interested in is Jansi: http://jansi.fusesource.org/

Jansi interprets ANSI code and format them for the console output. It works for both unix and windows.

Update 11/2014: you can also see the github Page

AdrieanKhisbe
  • 3,719
  • 7
  • 35
  • 45
Jcs
  • 12,641
  • 5
  • 49
  • 68
1

Not directly related to Java console output, but if you're looking to use ANSI colors in Kotlin console output, this is a great library to use - https://github.com/importre/crayon

nazmul idris
  • 372
  • 5
  • 16
  • 1
    Ha - Kotlin hadn't been released yet when I asked this question! It would be announced 2 months later and I wouldn't actually learn of it until several years later. – chandsie Aug 11 '18 at 21:03