Show System.out.println output with another color
Solution 1
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...
Solution 2
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");
}
}
Solution 3
Please have a look at Jansi (Jansi's Github)
Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows.
Wassim AZIRAR
https://www.malt.fr/profile/wassimazirar I am not looking for permanent contract (no need to contact me for that) I develop 💻 in .NET Core, JavaScript, Angular and React ⚛
Updated on July 09, 2022Comments
-
Wassim AZIRAR almost 2 years
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 eclipsefor 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
with syserr I have this
-
Jon Skeet over 12 years@OpenMind: Yes, but in a totally different way. What do you mean by "I don't have the same result" before?
-
Jon Skeet over 12 years@OpenMind: Try flushing after each write - I suspect it's a buffering issue.
-
Wassim AZIRAR over 12 yearsNot working, I also tried your Jansi library. There no method called printRed() !
-
Jon Skeet over 12 years@OpenMind: "my" Jansi library? I don't have any library for ANSI escape sequences - I was suggesting that you wrote one yourself: "you could just create a class"...
-
Jon Skeet over 12 years(Having said which, it sounds like Jansi already has the functionality you want - just not encapsulated as I described. Will edit a reference into the answer.)
-
Eagle over 8 yearsU can use this: java2s.com/Code/Jar/j/Downloadjansi14jar.htm if you want to use a jar instead of the archive. This resolved my import problem in eclipse.
-
Hugo Zaragoza over 8 yearsFor Elipse Luna and MacOS 10.10, the only thing that worked for me is this: mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console
-
Marcus Junius Brutus over 8 yearsactually
\033[0m
stands for reset. -
doc about 7 yearsBrilliant! I think this should be the accepted answer
-
Dee about 7 yearsi don't get the colour, only "?[32mSome test text?[0m" displayed in my eclipse console
-
Nor.Z over 2 years> They want to print colored text in the Eclipse Console, not the Windows Command Prompt or terminal. Eclipse Console is not an ansi compatible console. stackoverflow.com/questions/51413044/…