How to Print Color in Console Using System.Out.Println

How to color System.out.println output?

No, but there are third party API's that can handle it

http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html

Edit: of course there are newer articles than that one I posted, the information is still viable though.

Above link is dead, see this question instead: How to print color in console using System.out.println?

Show System.out.println output with another color

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...

Change color of java console output

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

Is there a way i can change the print out color of my Text?

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

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

This will print in red in the eclipse console.



Related Topics



Leave a reply



Submit