user8242198
user8242198

Reputation:

How to print colored text? [Eclipse Photon]

I want to use System.out.println to print colored text. I am using the latest version of Java in Java Eclipse Photon. I've looked up other tutorials but none have helped. I do not want to use plugins but rather anything in the standard JDK. Any help is appreciated.

Upvotes: 0

Views: 6299

Answers (2)

Ganesh Patel
Ganesh Patel

Reputation: 450

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:

  1. Standard out color
  2. Standard error color
  3. Console Background
  4. Standard In color

See the image for more detail (I think this setting is also work in Photon) enter image description here

Upvotes: 2

Jason Heithoff
Jason Heithoff

Reputation: 518

I don't think Eclipse console supports colors declared from Java output. Please see http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Frun-debug%2Fref-console.htm

The docs only show how to change the text color as a whole. However you can have different colors for standard out:

System.out.println("standard out");

vs Standard error

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

The following is a solution for both Mac and Linux terminals:

Create the following class

public class ANSIConstants {
  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";
}

Then you can do the following

System.out.println(ANSIConstants.ANSI_PURPLE + "example" + ANSIConstants.ANSI_RESET);

Upvotes: 3

Related Questions