11

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?

1

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

9

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";

1 Comment

Don't forget to add usage: System.out.println( " This will be purple: " + PURPLE + "Purple!" + RESET + " normal color text" );
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

Comments

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

1 Comment

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.
1

I needed colors for a project. Here is a class for everyone. Just do Colors.(color) + "whatever" to add color, bold, or italic. Use Colors.reset to reset the colors. Hope this helps.

package util;

public class Colors {
    public static final String reset = "\u001B[0m";
    public static final String bold = "\u001b[1m";
    public static final String italic = "\u001b[3m";
    public static final String underline = "\u001b[4m";
    public static final String reversed = "\u001b[7m";
    public static final String black = "\u001b[30m";
    public static final String blue = "\u001b[34m";
    public static final String cyan = "\u001b[36m";
    public static final String green = "\u001b[32m";
    public static final String magenta = "\u001b[35m";
    public static final String red = "\u001b[31m";
    public static final String white = "\u001b[37m";
    public static final String yellow = "\u001b[33m";
    public static final String brightBlack = "\u001b[30;1m";
    public static final String brightBlue = "\u001b[34;1m";
    public static final String brightCyan = "\u001b[36;1m";
    public static final String brightGreen = "\u001b[32;1m";
    public static final String brightMagenta = "\u001b[35;1m";
    public static final String brightRed = "\u001b[31;1m";
    public static final String brightWhite = "\u001b[37;1m";
    public static final String brightYellow = "\u001b[33;1m";
    public static final String bgBlack = "\u001b[40m";
    public static final String bgBlue = "\u001b[44m";
    public static final String bgCyan = "\u001b[46m";
    public static final String bgGreen = "\u001b[42m";
    public static final String bgMagenta = "\u001b[45m";
    public static final String bgRed = "\u001b[41m";
    public static final String bgWhite = "\u001b[47m";
    public static final String bgYellow = "\u001b[43m";
    public static final String bgBrightBlack = "\u001b[40;1m";
    public static final String bgBrightBlue = "\u001b[44;1m";
    public static final String bgBrightCyan = "\u001b[46;1m";
    public static final String bgBrightGreen = "\u001b[42;1m";
    public static final String bgBrightMagenta = "\u001b[45;1m";
    public static final String bgBrightRed = "\u001b[41;1m";
    public static final String bgBrightWhite = "\u001b[47;1m";
    public static final String bgBrightYellow = "\u001b[43;1m";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.