1

For Example

String text = "sample";
System.out.println(text);

I want the text to appear in color RED.Tried java.awt but a bit confusing for me.

Joe
  • 147
  • 1
  • 4
  • 17
  • 6
    Possible duplicate of [Show System.out.println output with another color](http://stackoverflow.com/questions/7091003/show-system-out-println-output-with-another-color) – amkz Jan 14 '16 at 12:20
  • Thanks for the link unfortunately, System.out.println("\033[31m RED"); did not work for me..... but System.err.println("sample"); worked. sample was printed out in Red color. – Joe Jan 14 '16 at 12:26
  • You can add ANSI color codes in most terminals: [http://stackoverflow.com/questions/5762491/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) – Gilfoyle Jan 14 '16 at 12:27
  • That didn't work for me reasons i know not. but below is what i did. `public String changeTextColor(String text) { System.err.println(text); return text; } log.log(changeTextColor(Adam)); ` This printed Adam out in red color. – Joe Jan 14 '16 at 12:43

3 Answers3

4

In case you are using eclipse... you can use

System.err.println("...");

This will print in red in the eclipse console.

1

You can change ANSI default color. Lets say you have 2 colors

String Green = "\u001B[32m";
String Defauld = "\u001B[0m";

One string ,
String word="Hello World";

and a printing code

System.out.print(Green + word + Defauld + "the end .");

And it print in Green.I dont know if this works in every system.Works in my Linux

PS:I dont have access in a pc right now could you please try the following in windows??

String Green = "\x1b[31m;
String Defauld = "\x1b[0m";
  • ....Dont think this works on windows. ANSI wont work with eclipse. System.out.print(Green + word + Defauld + "the end ."); prints out : "[32mword[0m the end". – Joe Jan 14 '16 at 13:01
  • Throws an error @ "\x "invalid escape sequence" – Joe Jan 14 '16 at 14:40
0

Another solution is to use JCDP (Java Colored Debug Printer) which works on both Windows and Linux. Response from here https://stackoverflow.com/a/6957561 .

GitHub: https://github.com/dialex/JCDP#downloads I have just tried the example in IntelliJ on Windows 7 and here's the result: enter image description here

Community
  • 1
  • 1
Johnny C
  • 709
  • 1
  • 8
  • 12