Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show System.out.println output with another color

I have a big project to debug, and I was wondering if there is anyway I could use to change the System.out.println method in the output of eclipse

for example :

System.out.println("I want this to be red");
System.out.println("I want this to be blue");
System.out.println("I want this to be yellow");
System.out.println("I want this to be magenta");

for more readability.

EDIT

with sysout I have this sysout

with syserr I have this syserr

like image 552
Wassim AZIRAR Avatar asked Aug 17 '11 10:08

Wassim AZIRAR


People also ask

How do I change the color of my output in Java?

Syntax: System. out. println(ANSI_COLORNAME + "This text is colored" + ANSI_RESET);

How do I print in color in eclipse?

In eclipse(Neon) Go to Windows > Preferences then search for Run/Debug after that click on Console under the Run/Debug. There are four option available for changing the console view: Standard out color. Standard error color.


2 Answers

Within Eclipse, the simplest approach would be to use System.err.println for lines you want to be in red - I believe that's the default. (You can change it in Preferences -> Run/Debug -> Console).

That difference won't show up when running in a real console of course, but I don't think the Eclipse console supports ANSI colour escape sequences etc.

EDIT: For the Windows console, I'd expect ANSI escape sequences to work. It's not hugely portable, but if that's not a problem, you could just create a class to encapsulate the escape sequences appropriately, so you could call something like:

ansiConsole.printRed("sample line in red");
ansiConsole.printBlue("sample line in blue");

(I'd probably make those methods return back to whatever the "current" colour was after each call.)

EDIT: As noted in comments, the Jansi library already exists, so you might as well use that. It doesn't have the methods described above, but I'm sure it'll still do what you want...

like image 74
Jon Skeet Avatar answered Sep 21 '22 15:09

Jon Skeet


Please Refer the following code.Also refer this link for ANSI color codes. http://en.wikipedia.org/wiki/ANSI_escape_code

public class ColourConsoleDemo {
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("\033[0m BLACK");
        System.out.println("\033[31m RED");
        System.out.println("\033[32m GREEN");
        System.out.println("\033[33m YELLOW");
        System.out.println("\033[34m BLUE");
        System.out.println("\033[35m MAGENTA");
        System.out.println("\033[36m CYAN");
        System.out.println("\033[37m WHITE");
    }   
}
like image 33
Sachin Gaykar Avatar answered Sep 21 '22 15:09

Sachin Gaykar