I have my Java project set up in Eclipse to use UTF-8 encoding. When I run the code within the IDE, it is run with UTF-8. However, when I bundle my Java project into an OS X app and run it from there, the Java code is run with MacRoman, as confirmed by Charset.defaultCharset()
.
How can I make my bundled OS X app run in UTF-8?
This answer notes that the character encoding is determined by the JVM as it starts up. If this were being run from a command line, you would be able to set UTF-8 encoding by adding the runtime parameter -Dfile.encoding=UTF-8
Since this is being run in a bundled app, you aren't able to add this parameter in the Terminal. Fortunately, Apple has made it pretty easy to input the above parameter in an alternate way while starting up the bundled app.
First, use a text editor (TextEdit works) to open up the Info.plist file within your application bundle (right-click on app and choose "Show Package Contents", then open the "Contents" folder). You will see a section that looks something like this:
<key>Java</key>
<dict>
<key>JVMVersion</key>
<string>1.5+</string>
<key>MainClass</key>
<string>com.yourCompanyName.YourApplication</string>
<key>Properties</key>
<dict>
<key>apple.laf.useScreenMenuBar</key>
<string>true</string>
</dict>
<key>VMOptions</key>
<string>-Xmx512m</string>
</dict>
The import part is the <key>Properties</key>
part. If it doesn't exist, create it and the <dict>
immediately beneath it. The add the following key and value between the <dict>
tags:
<key>file.encoding</key>
<string>UTF-8</string>
For future edification, you can add any system properties in this way. If you have a parameter like at the beginning of this answer, simply drop the leading -D
, put the part before the equals in the <key>
, and the part after the equals in the <string>
.