2008-05-10

Getting Unicode output in Eclipse Console

Tired of seeing garbled Eclipse Console output? Here is a quick and dirty tutorial for getting Unicode output in Eclipse Console.

Fact: You will not get Unicode output in Eclipse Console while using System.out directly in Windows. See Eclipse BUG #13865.

1. add -Dfile.encoding=UTF-8 to your eclipse.ini

2. make sure your Eclipse Console font supports Unicode. You can try it out by typing unicode characters directly to console with keyboard. Console Font is set in Window -> Preferences -> General -> Appearance -> Colors and Fonts -> Debug -> Console Font

3. if you are NOT using Windows, set your system encoding to UTF-8. You should now see Unicode characters in Console after restarting Eclipse.

4. if you are using Windows or do not want to change your OS encoding, you will have to avoid using System.out stream directly. Instead, wrap it up with java.io.PrintStream:
PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
sysout.println("\u2297\u0035\u039e\u322F\u5193");


5. if you are using Log4J with ConsoleAppender, make sure to set the encoding property to UTF-8. Example:
#TRACE appender
log4j.appender.stdout.trace=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.trace.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.trace.encoding=UTF-8
log4j.appender.stdout.trace.layout.ConversionPattern=%p [%c] - %m%n
log4j.appender.stdout.trace.Threshold=TRACE


Happy development!

16 comments:

  1. This was helpful in solving a problem I was having, thanks. But changing eclipse.ini didn't work for me.



    Instead, I altered the JRE configuration :

    Window > Preferences > Java > Installed JREs ; select JRE ; select Edit

    add to Default VM Arguments: -Dfile.encoding=UTF-8

    ReplyDelete
  2. you are wrong it is possible to have UTF-8 for windows with System.out simple:

    refer: http://bytecounts.com/blog/?p=28

    rules are actual for Windows too.

    ReplyDelete
  3. akhlystov, setting unicode font does not solve system encoding problems (read more about it: https://bugs.eclipse.org/bugs/show_bug.cgi?id=13865)

    This may not be an issue with Windows Vista, though I haven't tried it there yet, only on XP. Anyway, it's better to use loggers instead of printing to System.out.

    ReplyDelete
  4. No I'm not talking about fonts. I'm talking about
    1. Open Run Dialog > "your application" > Common Tab > Console Encoding > Other "set it to UTF-8"

    2. Open Run Dialog > "your application" > Arguments Tab > VM Arguments > Add "-Dfile.encoding=UTF-8"

    and of course Window Menu > General > Workspace > Text file encoding should be set to "UTF-8"

    then you can run your application ( doesn't matter Standalone or inside Tomcat)

    e.g. this code will work:

    public class Hello {

    public static void main(String [] args)
    {
    System.out.println("english letters. русские буквы");
    System.out.println("new Date()= " + new Date());
    }

    ReplyDelete
  5. Thank you!
    I didn't need to alter the Console Font, only added -Dfile.encoding=UTF-8 to eclipse.ini

    ReplyDelete
  6. Thank you all for the comments. Adding -Dfile.encoding=UTF-8 to eclipse.ini wroked for me as well.

    ReplyDelete
  7. thanks, worked on OS X, added -Dfile.encoding=UTF-8 to /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini

    ReplyDelete
  8. Thank you, this was unbelievably helpful!

    ReplyDelete
  9. Thanks a lot, this problem confused me for long days, altering eclipse.ini solved everything.

    ReplyDelete
  10. This one's better, guys :

    http://i18ncookbook.com/eclipse_settings

    ReplyDelete
  11. Thanks; worked great - glad to finally be able to see what I'm doing in the console.

    ReplyDelete
  12. Thanks!
    But instead of changing .ini file i did that in eclipse: Window -> Preferences-> General -> Workspace -> Text File Encoding -> Other UTF-8

    That was sufficient for me.

    Now i get nice error messages in russian :)

    ReplyDelete
  13. I'm an Eclipse newbie, so take this with a grain of salt, but I managed to get around the problem in Eclipse Indigo SR1 simply by going to Edit > Set Encoding, then choosing UTF-8.

    ReplyDelete
  14. Thanks again! You've helped out me second time:)

    ReplyDelete
  15. This code is great:

    PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
    sysout.println("\u2297\u0035\u039e\u322F\u5193");

    Thanks!

    DJ Guo

    ReplyDelete

Spam comments (i.e. ones that contain links to web development services) will be reported along with user profiles!

Note: only a member of this blog may post a comment.