1

我在 Eclipse 中设置了我的 Java 项目以使用 UTF-8 编码。当我在 IDE 中运行代码时,它使用 UTF-8 运行。但是,当我将 Java 项目捆绑到 OS X 应用程序并从那里运行时,Java 代码将使用 MacRoman 运行,如Charset.defaultCharset().

如何让捆绑的 OS X 应用程序以 UTF-8 运行?

4

1 回答 1

1

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>.

于 2013-11-14T06:36:43.873 回答