Change color of java console output
Update 11/2014: you can also see the github Page Solution 4: 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 Question: I try to generate colored console output using ANSI escape codes with the following minimal C# program: I am running Ansicon v1.66 on Windows 7 x64 with csc.exe ( Microsoft (R) Visual C# Compiler version 4.6.0081.0 ). 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.
Change color of java console output
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?
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
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";
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
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
How to use the ANSIcolor plugin in Jenkins?, The ANSI-color plug in transforms your console output to HTML. If your output contains ansi escape sequences, then it converts these special char sequences into (colored) HTML. You can only configure the mapping via ‘ANSI color map’. Example: \x1b[31m is transformes html color red . It looks that your program …
ANSI-Coloring Console Output with .NET
I try to generate colored console output using ANSI escape codes with the following minimal C# program:
using System; // test.cs class foo < static void Main(string[] args) < Console.WriteLine("\x1b[36mTEST\x1b[0m"); >>
I am running Ansicon v1.66 on Windows 7 x64 with csc.exe ( Microsoft (R) Visual C# Compiler version 4.6.0081.0 ).
Colored output works fine in this configuration; Ansicon itself is working flawlessly.
To cross-check I use a node.js one-liner that is 100% equivalent to the C# program:
// test.js console.log("\x1b[36mTEST\x1b[0m");
And, even more basic, a hand-crafted text file:
Both of which which correctly do the expected thing: Print a teal -colored string «TEST»:
Only the test.exe I built with csc prints something else. Why?
I’ve created a small plugin (available on NuGet) that allows you to easily wrap your strings in ANSI color codes. Both foreground and background colors are supported.
It works by extending the String object, and the syntax is very simple:
After which the string is ready to be printed to the console.
Your program needs to be compiled for /platform:x64 if you use the AnsiCon x64 environment and with /platform:x86 if you use the AnsiCon x86/32 bits version. The exact reason is a mystery.
Originally I thought you need all this:
You need to grab the StandardOutput and let the Console.WriteLine believe you write to a File instead of to a Console and use an ASCII encoding.
var stdout = Console.OpenStandardOutput(); var con = new StreamWriter(stdout, Encoding.ASCII); con.AutoFlush = true; Console.SetOut(con); Console.WriteLine("\x1b[36mTEST\x1b[0m");
The .Net Console.WriteLine uses an internal __ConsoleStream that checks if the Console.Out is as file handle or a console handle. By default it uses a console handle and therefor writes to the console by calling WriteConsoleW. In the remarks you find:
Although an application can use WriteConsole in ANSI mode to write ANSI characters, consoles do not support ANSI escape sequences. However, some functions provide equivalent functionality. For more information, see SetCursorPos, SetConsoleTextAttribute, and GetConsoleCursorInfo.
To write the bytes directly to the console without WriteConsoleW interfering a simple filehandle/stream will do which is achieved by calling OpenStandardOutput . By wrapping that stream in a StreamWriter so we can set it again with Console.SetOut we are done. The byte sequences are send to the OutputStream and picked up by AnsiCon.
Do notice that this is only useable with an applicable terminal emulator, like AnsiCon, as shown here:
I encountered this question today and I could not get the accepted answer to work. After some research on my own I found an answer which will get it to work.
It is a pitty, but we need to go very low level on this and call the Windows API directly. For this purpose I’m using the PInvoke.Kernel32 NuGet for convenience reasons, but if it is too heavy-weight for you, you might create the P\Invoke mapping yourself.
The following method illustrates, how one may activate the ANSI Codes:
bool TryEnableAnsiCodesForHandle(Kernel32.StdHandle stdHandle)
To enable it for StdOut you call it like:
TryEnableAnsiCodesForHandle(Kernel32.StdHandle.STD_OUTPUT_HANDLE);
If the method returns true, the ANSI Codes are enabled, else they are not.
The solution uses the very low level Windows API GetConsoleMode and SetConsoleMode to check if the control buffer mode ENABLE_VIRTUAL_TERMINAL_PROCESSING is set, and if it is not set it tries to set the mode.
How to Print Colored Text in Java Console?, Remember that in Java Programming the background color and text color of the output screen is black or white by default. If we want to Highlight some text on the output screen then we can use the ANSI color codes and highlight the particular text. One can refer to the ANSI escape code in order to explore more. …
JavaScript: Display the colors entered in an array by a specific format
JavaScript Array: Exercise-15 with Solution
Write a JavaScript program to display the colors in the following way.
Here is the sample array:
color = [«Blue «, «Green», «Red», «Orange», «Violet», «Indigo», «Yellow «];
o = [«th»,»st»,»nd»,»rd»]
Output
«1st choice is Blue .»
«2nd choice is Green.»
«3rd choice is Red.»
— — — — — — — — — — — — —
Sample Solution:
JavaScript Code:
var color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow "];function Ordinal(n) < var o = ["th","st","nd","rd"], x = n%100; return x+(o[(x-20)%10]||o[x]||o[0]); >for(n = 0; n
1st choice is Blue . 2nd choice is Green. 3rd choice is Red. 4th choice is Orange. 5th choice is Violet. 6th choice is Indigo. 7th choice is Yellow .
ES6 Version:
const color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow "];function Ordinal(n) < const o = ["th","st","nd","rd"]; const x = n%100; return x+(o[(x-20)%10]||o[x]||o[0]); >for(n = 0; n < color.length; n++)< const ordinal = n + 1; const output = (`$choice is $.`);console.log(output); >
See the Pen JavaScript — Display the colors entered in an array by a specific format — array-ex- 15 by w3resource (@w3resource) on CodePen.
Previous: Write a JavaScript program to remove duplicate items from an array (ignore case sensitivity).
Next: Write a JavaScript program to find the leap years from a given range of years.
Java — Making a log4j console appender use different, Added a more advanced example of log4j and a example output. I’m quite pleased with this solution, and I wanted to save someone else having to deal with ansi colors, logging levels, conversion patterns colors and testing. Tested in Ansi Console on Eclipse and ANSICON. –
How to Print Colored Text in Java Console?
Remember that in Java Programming the background color and text color of the output screen is black or white by default. If we want to Highlight some text on the output screen then we can use the ANSI Color codes and highlight the particular text. One can refer to the ANSI escape code in order to explore more.
System.out.println(ANSI_COLORNAME + "This text is colored" + ANSI_RESET);
As perceived from the above syntax contains This Syntax contains 3 parts:
- In ANSI_COLORNAME we have to write the name in which we have given the particular ANSI code. for e.g public static final String ANSI_BLACK = “\u001B[30m”;
The above is pseudo-code is to print text in black color. So here we can use ANSI_BLACK in place of ANSI_COLORNAME to print the text in Black color.
- The second part is to write the text which we want to print in that color.
- The ANSI_RESET code turns off all ANSI attributes set so far, which should return the console to its defaults.
Color Name | Color code | Background Color | Background Color code |
---|---|---|---|
BLACK | \u001B[30m | BLACK_BACKGROUND | \u001B[40m |
RED | \u001B[31m | RED_BACKGROUND | \u001B[41m |
GREEN | \u001B[32m | GREEN_BACKGROUND | \u001B[42m |
YELLOW | \u001B[33m | YELLOW_BACKGROUND | \u001B[43m |
BLUE | \u001B[34m | BLUE_BACKGROUND | \u001B[44m |
PURPLE | \u001B[35m | PURPLE_BACKGROUND | \u001B[45m |
CYAN | \u001B[36m | CYAN_BACKGROUND | \u001B[46m |
WHITE | \u001B[37m | WHITE_BACKGROUND | \u001B[47m |
Illustration: Text coloring in Java:
Coloring Java output on the console
Have you ever thought of coloring the output on the console through Java? Well, recently I wanted to color my console output and I’ve got the program working finally after a bit of research.
Just to give you a bit of background about consoles, each console vendor used to have their own standards in the early days. And ANSI had finally introduced a standard for consoles to interpret the colors, positions, control characters etc. You can read more about ANSI escape codes on wiki.
As per the ANSI standard all the instructions to interpret the color and positions should follow below syntax:
- Output text is the output that should be printed on the console
- Code represents the effect that should take place on console. Refer below table (Note, I’m giving only very commonly used codes here)
Note: DOS doesn’t support ANSI colors. You can use Linux terminal or Netbeans IDE to run this program. And Escape character in Java is ASCII code 33 (i.e.; \033)
Coloring.java
/**
* Demonstrating console colring
* @author SANTHOSH REDDY MANDADI
* @version 1.0
* @since 06-June-2013
*/
public class Coloring
public static void main(String args[])
System.out.println("\033[31;1mHello\033[0m, \033[32;1;2mworld!\033[0m");
System.out.println("\033[31mRed\033[32m, Green\033[33m, Yellow\033[34m, Blue\033[0m");
>
>
Hello, world!
Red, Green, Yellow, Blue