java: colorful system.out messages on console
I would like my command line java program to output colored texts into the unix console. I am specifically using gnome-terminal on Ubuntu 10.4.
I am able to get colors with something like echo "\033[01;32m"Hello
on the terminal.
How can I trigger this wit开发者_JAVA技巧h java code? Thanks
If you don't care about terminal compatibility, just replace echo
with System.out.println(
above. For example,
System.out.println("\033[01;32mHello\n");
The color of text is at the OS layer so I think you can do it using JNI call.
Try this example
Note: make unix equivalent of that,
OR
javacurses is also helpful in your case
OR
enigma-shell is also helpful
This would do the trick:
Process p = Runtime.getRuntime().exec("echo -e \"\\033[01;32m\"Could Not Add The Task!");
Then redirect the inputStream into the System.out like this:
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
精彩评论