Thursday, 21 April 2011

Setting the Default Java File Encoding to UTF-8 on a Mac

Recently had a lot of pain with tests containing non-ASCII characters; both Fitnesse and the GMaven* plugin for Maven were failing to read UTF-8 files correctly because they were picking up the MacRoman character set that is the default for Java on the Mac.

You can start java up with an argument -Dfile.encoding=UTF-8, but that's a pain at best and very difficult if using tools that start JVMs up themselves.

However, this answer on stackoverflow gave me a way to set it for all Java everywhere on the Mac:

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8

If you want this to be persistent, then add this property as explained in this other answer on stackoverflow.

* There's a bug in GMaven 1.3 - it doesn't obey the project.build.sourceEncoding property in Maven, for test files at any rate