2

I am fairly new to Java and need your help. I am trying to change the color of the text in Eclipse console on run-time. I referred to one of the posts and tried to follow that. But the output shows a box of unicode escape character and the color of the text remains unchanged.

public static void main(String[] args) 
{
    System.out.println((char)27 + "[31mTest red color");
}

OUTPUT:

|00|

|1B| [31mTest red color

Notes:

  1. For output, I couldn't upload the image so i have typed [001B].
  2. The console uses UTF-8 encoding.

UPDATE Following the comment from Jarrod for possible duplication of the question, here is my update - The question is mainly for ANSI character not working and not for text color, so i have updated the question.

ElevatedLyf
  • 116
  • 1
  • 2
  • 8
  • 1
    What OS are you using? Those are ANSI escape sequences and don't work on every OS. – Kevin Workman Feb 23 '15 at 18:14
  • If you really want this kind of functionality, you should probably just build a GUI. Check out Swing and JavaFX. – Kevin Workman Feb 23 '15 at 18:15
  • Hi Kevin, Thanks for your reply! I use eclipse on Ubuntu Linux. I have not built any API but i will search and play around with Swing, JavaFX and see if it works. – ElevatedLyf Feb 23 '15 at 21:06
  • Try using a ANSI-capable terminal, like gnome-terminal emulator. It works, but you are probably seeing the results from Eclipse output window, which does not support ANSI escapes. – Stefano Sanfilippo Feb 23 '15 at 22:39
  • @KevinWorkman apart from GUIs, having command line tools capable of colour output is a good thing in 2015. – Stefano Sanfilippo Feb 23 '15 at 22:44
  • @StefanoSanfilippo If you say so. My only point is that since not every OS supports this functionality, you might be better off creating a basic GUI, if you really care about it. – Kevin Workman Feb 23 '15 at 23:34

1 Answers1

2

Eclipse does not support ANSI escape characters out of the box. You have to install a plugin for that: https://marketplace.eclipse.org/content/ansi-escape-console

Pait
  • 757
  • 6
  • 19