如何强制jar使用(或jar运行的jvm)utf-8而不是系统的默认编码

发布于 2024-10-01 11:50:01 字数 278 浏览 1 评论 0原文

我的Windows默认编码是GBK,我的Eclipse完全是utf-8编码。
因此,在我的 Eclipse 中运行良好的应用程序崩溃了,因为导出为 jar 文件时单词变得不可读;
我必须在 .bat 文件中编写以下行来运行应用程序

   start java -Dfile.encoding=utf-8 -jar xxx.jar    

现在我的问题是,我可以在源代码中编写一些内容来设置应用程序使用(或 jvm 运行)utf-8 而不是系统的默认编码。

My Windows's default encoding is GBK, and my Eclipse is totally utf-8 encoded.
So an application which runs well in my Eclipse, crashes because the words become unreadable when exported as a jar file;
I have to write the following line in a .bat file to run the application

   start java -Dfile.encoding=utf-8 -jar xxx.jar    

Now my question is that can I write something in the source code to set the application uses(or the jvm runs in) utf-8 instead of the system's default encoding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难以启齿的温柔 2024-10-08 11:50:01

当您打开文件进行读取时,您需要显式指定要用于读取文件的编码:

Reader r = new InputStreamReader(new FileInputStream("myfile"), StandardCharsets.UTF_8);

然后是默认平台编码的值(您可以使用 -Dfile.encoding 进行更改)不再重要了。

注意:

我通常建议始终为任何依赖于标准语言环境的操作(例如字符 I/O)显式指定编码。许多 Java API 方法默认使用平台编码,我认为这是一种糟糕的设计,因为平台编码通常不是正确的,而且它可能会突然发生变化(例如,如果用户切换操作系统区域设置),从而破坏您的应用程序。

所以只要说出你想要哪种编码即可。

在某些情况下,平台编码是正确的(例如打开用户刚刚为您创建的文件时),但这种情况相当罕见。

注释 2

java.nio.charset.StandardCharsets 是在 Java 1.7 中引入的。对于较旧的 Java 版本,您需要将输入编码指定为字符串(呃)。可能的编码列表取决于 JVM,但保证每个 JVM 至少具有:

US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16。

When you open a file for reading, you need to explicitly specify the encoding you want to use for reading the file:

Reader r = new InputStreamReader(new FileInputStream("myfile"), StandardCharsets.UTF_8);

Then the value of the default platform encoding (which you can change using -Dfile.encoding) no longer matters.

Note:

I would normally recommend to always specify the encoding explicitly for any operation that depends on the standard locale, such as character I/O. Many Java API methods default to the platform encoding, which I consider a bad design, because often the platform encoding is not the right one, plus it may suddenly change (if the user e.g. switches OS locale), breaking your app.

So just always say which encoding you want.

There are some cases where the platform encoding is the right one (such as when opening a file the user just created for you), but they are fairly rare.

Note 2:

java.nio.charset.StandardCharsets was introduced in Java 1.7. For older Java versions, you need to specify the input encoding as a String (ugh). The list of possible encodings depends on the JVM, but every JVM is guaranteed to at least have:

US-ASCII, ISO-8859-1,UTF-8,UTF-16BE,UTF-16LE,UTF-16.

铁轨上的流浪者 2024-10-08 11:50:01

还有另一种方法。
如果您确定如何对输入和输出进行编码,则可以在编译 jar 文件之前保存设置。

以下是 NetBeans 的示例。

转到项目>>属性>>运行>> VM 选项并输入 -Dfile。 coding=UTF-8

之后,每次启动 Java VM 时,所有内容都会以 UTF-8 进行编码。

(我认为 Eclipse 提供了相同的可能性。如果没有,只需 google 到 VM Options。)

在此处输入图像描述

There's another way.
If you are sure how you like to encode the input and output, you can save the settings before you compile your jar file.

Here is a example for NetBeans.

Go To Project >> Properties >> Run >> VM Options and type -Dfile. encoding=UTF-8

After that, everything is encoded in UTF-8 every time the Java VM is started.

(I think Eclipse offers the same possibility. If not, just google to VM Options.)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文