简体   繁体   English

捆绑的Java OS X App默认为MacRoman编码

[英]Bundled Java OS X App Defaults to MacRoman Encoding

I have my Java project set up in Eclipse to use UTF-8 encoding. 我在Eclipse中设置了Java项目以使用UTF-8编码。 When I run the code within the IDE, it is run with UTF-8. 当我在IDE中运行代码时,它将与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() . 但是,当我将Java项目捆绑到OS X应用程序中并从那里运行它时,如Charset.defaultCharset()所确认,Java代码是使用MacRoman运行的。

How can I make my bundled OS X app run in UTF-8? 如何使捆绑的OS X应用程序在UTF-8中运行?

This answer notes that the character encoding is determined by the JVM as it starts up. 该答案指出,字符编码由JVM启动时确定。 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 如果是从命令行运行的,则可以通过添加运行时参数-Dfile.encoding=UTF-8来设置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. 幸运的是,Apple在启动捆绑的应用程序时非常容易以另一种方式输入上述参数。

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). 首先,使用文本编辑器(TextEdit可以工作)打开应用程序捆绑包中的Info.plist文件(右键单击应用程序并选择“显示包内容”,然后打开“内容”文件夹)。 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. 导入部分是<key>Properties</key>部分。 If it doesn't exist, create it and the <dict> immediately beneath it. 如果不存在,请创建它,并在它下面直接创建<dict> The add the following key and value between the <dict> tags: <dict>标签之间添加以下键和值:

<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> . 如果您在此答案的开头具有类似的参数,只需将前导-D放下,在<key>中将等号之前的部分放在<string> ,将等号之后的部分放入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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