简体   繁体   中英

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 with java code? Thanks

如果您不关心终端兼容性,只需将echo替换为System.out.println(上面。例如,

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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM