How to print color in console using System.out.println?
Solution 1
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
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";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
Solution 2
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note
Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = "\033[0m"; // Text Reset
// Regular Colors
public static final String BLACK = "\033[0;30m"; // BLACK
public static final String RED = "\033[0;31m"; // RED
public static final String GREEN = "\033[0;32m"; // GREEN
public static final String YELLOW = "\033[0;33m"; // YELLOW
public static final String BLUE = "\033[0;34m"; // BLUE
public static final String PURPLE = "\033[0;35m"; // PURPLE
public static final String CYAN = "\033[0;36m"; // CYAN
public static final String WHITE = "\033[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = "\033[1;30m"; // BLACK
public static final String RED_BOLD = "\033[1;31m"; // RED
public static final String GREEN_BOLD = "\033[1;32m"; // GREEN
public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW
public static final String BLUE_BOLD = "\033[1;34m"; // BLUE
public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE
public static final String CYAN_BOLD = "\033[1;36m"; // CYAN
public static final String WHITE_BOLD = "\033[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK
public static final String RED_UNDERLINED = "\033[4;31m"; // RED
public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK
public static final String RED_BRIGHT = "\033[0;91m"; // RED
public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN
public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE
}
Solution 3
I created a library called JColor that works on Linux, macOS, and Windows 10.
It uses the ANSI codes mentioned by WhiteFang, but abstracts them using words instead of codes which is more intuitive. Recently I added support for 8 and 24 bit colors 🌈
Choose your format, colorize
it, and print it:
System.out.println(colorize("Green text on blue", GREEN_TEXT(), BLUE_BACK()));
You can also define a format once, and reuse it several times:
AnsiFormat fWarning = new AnsiFormat(RED_TEXT(), YELLOW_BACK(), BOLD());
System.out.println(colorize("Something bad happened!", fWarning));
Head over to JColor github repository for some examples.
Solution 4
Try the following enum :
enum Color {
//Color end string, color reset
RESET("\033[0m"),
// Regular Colors. Normal color, no bold, background color etc.
BLACK("\033[0;30m"), // BLACK
RED("\033[0;31m"), // RED
GREEN("\033[0;32m"), // GREEN
YELLOW("\033[0;33m"), // YELLOW
BLUE("\033[0;34m"), // BLUE
MAGENTA("\033[0;35m"), // MAGENTA
CYAN("\033[0;36m"), // CYAN
WHITE("\033[0;37m"), // WHITE
// Bold
BLACK_BOLD("\033[1;30m"), // BLACK
RED_BOLD("\033[1;31m"), // RED
GREEN_BOLD("\033[1;32m"), // GREEN
YELLOW_BOLD("\033[1;33m"), // YELLOW
BLUE_BOLD("\033[1;34m"), // BLUE
MAGENTA_BOLD("\033[1;35m"), // MAGENTA
CYAN_BOLD("\033[1;36m"), // CYAN
WHITE_BOLD("\033[1;37m"), // WHITE
// Underline
BLACK_UNDERLINED("\033[4;30m"), // BLACK
RED_UNDERLINED("\033[4;31m"), // RED
GREEN_UNDERLINED("\033[4;32m"), // GREEN
YELLOW_UNDERLINED("\033[4;33m"), // YELLOW
BLUE_UNDERLINED("\033[4;34m"), // BLUE
MAGENTA_UNDERLINED("\033[4;35m"), // MAGENTA
CYAN_UNDERLINED("\033[4;36m"), // CYAN
WHITE_UNDERLINED("\033[4;37m"), // WHITE
// Background
BLACK_BACKGROUND("\033[40m"), // BLACK
RED_BACKGROUND("\033[41m"), // RED
GREEN_BACKGROUND("\033[42m"), // GREEN
YELLOW_BACKGROUND("\033[43m"), // YELLOW
BLUE_BACKGROUND("\033[44m"), // BLUE
MAGENTA_BACKGROUND("\033[45m"), // MAGENTA
CYAN_BACKGROUND("\033[46m"), // CYAN
WHITE_BACKGROUND("\033[47m"), // WHITE
// High Intensity
BLACK_BRIGHT("\033[0;90m"), // BLACK
RED_BRIGHT("\033[0;91m"), // RED
GREEN_BRIGHT("\033[0;92m"), // GREEN
YELLOW_BRIGHT("\033[0;93m"), // YELLOW
BLUE_BRIGHT("\033[0;94m"), // BLUE
MAGENTA_BRIGHT("\033[0;95m"), // MAGENTA
CYAN_BRIGHT("\033[0;96m"), // CYAN
WHITE_BRIGHT("\033[0;97m"), // WHITE
// Bold High Intensity
BLACK_BOLD_BRIGHT("\033[1;90m"), // BLACK
RED_BOLD_BRIGHT("\033[1;91m"), // RED
GREEN_BOLD_BRIGHT("\033[1;92m"), // GREEN
YELLOW_BOLD_BRIGHT("\033[1;93m"), // YELLOW
BLUE_BOLD_BRIGHT("\033[1;94m"), // BLUE
MAGENTA_BOLD_BRIGHT("\033[1;95m"), // MAGENTA
CYAN_BOLD_BRIGHT("\033[1;96m"), // CYAN
WHITE_BOLD_BRIGHT("\033[1;97m"), // WHITE
// High Intensity backgrounds
BLACK_BACKGROUND_BRIGHT("\033[0;100m"), // BLACK
RED_BACKGROUND_BRIGHT("\033[0;101m"), // RED
GREEN_BACKGROUND_BRIGHT("\033[0;102m"), // GREEN
YELLOW_BACKGROUND_BRIGHT("\033[0;103m"), // YELLOW
BLUE_BACKGROUND_BRIGHT("\033[0;104m"), // BLUE
MAGENTA_BACKGROUND_BRIGHT("\033[0;105m"), // MAGENTA
CYAN_BACKGROUND_BRIGHT("\033[0;106m"), // CYAN
WHITE_BACKGROUND_BRIGHT("\033[0;107m"); // WHITE
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
And now we will make a small example:
class RunApp {
public static void main(String[] args) {
System.out.print(Color.BLACK_BOLD);
System.out.println("Black_Bold");
System.out.print(Color.RESET);
System.out.print(Color.YELLOW);
System.out.print(Color.BLUE_BACKGROUND);
System.out.println("YELLOW & BLUE");
System.out.print(Color.RESET);
System.out.print(Color.YELLOW);
System.out.println("YELLOW");
System.out.print(Color.RESET);
}
}
Solution 5
A fairly portable way of doing it is with the raw escape sequences. See http://en.wikipedia.org/wiki/ANSI_escape_code
[edited for user9999999 on 2017-02-20]
Java doesn't "handle the codes", that's true, but Java outputs what you told it to output. it's not Java's fault that the Windows console treats ESC (chr(27)) as just another glyph (←).
Taranath Datta
Updated on February 12, 2022Comments
-
Taranath Datta over 2 years
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
-
Boro about 13 years@WhiteFang34 Can you please explain what is the use of RESET if its color is BLACK, at least in my console? Is it like a default or sth.?
-
WhiteFang34 about 13 years@Boro: the reset code turns off all ANSI attributes set so far, which should return the console to its defaults. It's useful if you don't know the default color or are also using some of the other attributes like background color, font styles, etc.
-
Danny Lo about 10 yearsjansi is really great! for those who develop in eclipse, i can reccomend this plugin: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console and nice piece of code to enable color if the code isn't being executed in console:
if (System.console() == null) System.setProperty("jansi.passthrough", "true");
-
Pankaj Nimgade over 9 years@WhiteFang34 i am using windows and for some reason it is not working
-
Felix Edelmann over 8 years@PankajNimgade, read the answer again and you'll maybe notice this:
however it doesn't work for Windows command prompt
-
simpleuser over 7 yearswhich doesn't work because the Java IO layer does not convert those to colors. System.out.println((char)27 + "[31;1mERROR" + (char)27 + "[0m" only yields "[31;1mERROR[0m" when run from a windows cmd.com as an executable .jar
-
jcomeau_ictx over 7 yearsthe question wasn't tagged
windows
. the Windows console was never ANSI-compliant that I remember. -
simpleuser about 7 yearsbut the issue is that java isn't handling the codes, regardless of cmd.com's support
-
jcomeau_ictx about 7 yearssee edited answer. Java is doing exactly as it's told. the problem is the non-ANSI-compliant console.
-
Stéphane GRILLON about 6 yearsI have same problem
-
morbac about 6 yearsIt doesn't work on Windows it you're using cmd or powershell. Instead, you can use another console emulator. For instance, it works successfully with Cmder (cmder.net)
-
morbac about 6 yearslook at cmder.net ; this console works fine on Windows and allows colored text
-
Martin Krajčírovič almost 6 yearsSomeone shall notice that when copy pasting these colors into NetBeans will result with one extra slash added by NetBeans... So don't be super mad like me while trying this out :D
-
firephil about 5 yearsMaven uses this library to have colour in the console on any Operating system fusesource.github.io/jansi
-
jw_ over 4 yearsDoesn't work on eclipse Java version: 2018-12 (4.10.0) console windows during debugging. Indeed I want to output red text, for this purpose you can use System.err.println
-
help-info.de over 4 yearsThe "downvote" isn't mine - but, there are other answers that provide the OP's question, and they were posted some time ago. When posting an answer see: How do I write a good answer?, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions.
-
David over 4 years@iSahil this probably got downvoted because simply writing to standard error doesn't explicitly color anything. Many IDEs and consoles will interpret error messages and print them in red or similar, but that's not something you can rely on.
-
dialex over 4 yearsThis is a copy of answer stackoverflow.com/a/45444716/675577
-
Maude over 4 yearsWhile this wasn't the direct answer to the question asked above, this was the answer I was looking for when searching for "java print in red console". Thus, I do feel like it has it's place on this page.
-
Captain Jack Sparrow over 4 years@DannyLo Thank you so much for the link to the Eclipse plugin!
-
dan1st about 4 yearsAs far as I know, it should work with newer versions of windows 10 in conhost too as it supports ansi codes.
-
Alexander Stohr over 3 yearsOn Windows the Cygwin console does the job. Also when using piplines with e.g. "tee" then the ANSI color codes see interpretation and maybe effects as well. Byond color codes the ANSI escapes can also do e.g. cursor control.
-
Alexander Stohr over 3 yearsthis might need UTF or similar support. you are only providing the final results, not the things the coder needs to do for that. the answer might break the box of what the question spanned up.
-
M.A.S over 3 yearsIf I have
public static final String ANSI_RED = "\u001B[31m";
, how can I later get the color used for this specific String ? something likeANSI_RED.getColor();
? which I would want to return to meColor.RED
-
AmigoJack about 3 yearsNo, the variable has to be in the key
HKEY_CURRENT_USER\Console
directly - do not create another keyVirtualTerminalLevel
in there. -
meredrica over 2 yearsprinting to a different output stream just for color will get you in a multitude of problems
-
dialex over 2 yearsDownvoted, for the reasons mentioned by Alexander
-
Mojtaba Hosseini over 2 yearsAdded instructions for how to use emojis in different operating systems. @dialex
-
Mojtaba Hosseini over 2 years@AlexanderStohr I have added some more instructions. please tell me if you find any issues or better than that if you find solutions to complete the answer for the community 🙏
-
Avec over 2 yearsNice. Works as expected.
-
Avec over 2 years@M.A.S You could just create an Enum with all the unicode ASNI values.
-
aymens about 2 yearsSo wonderful! I loved it! Thank you! :)
-
CharlesC about 2 yearsworked great in IntelliJ console, thank you!
-
Enderbyte09 about 2 yearsYou can get colour in Windows cmd by running "color"
-
Anshuman Chatterjee about 2 yearsFor WINDOWS 10 users, you will need to ENABLE_VIRTUAL_TERMINAL_PROCESSING stackoverflow.com/questions/52767585/…