如何强制jar使用(或jvm运行)utf-8,而不是系统的默认编码 [英] How to Force a jar to uses(or the jvm runs in) utf-8 instead of the system's default encoding
问题描述
我的Windows的默认编码是GBK,我的Eclipse完全是utf-8编码的。
因此,在Eclipse中运行良好的应用程序崩溃,因为当导出为jar文件时,这些单词变得不可读;
我必须在.bat文件中编写以下行以运行应用程序
Dfile.encoding = utf-8 -jar xxx.jar
现在我的问题是,在源代码中设置应用程序使用(或jvm运行)utf-8而不是系统的默认编码。
当您打开要阅读的文件时,您需要明确指定要用于阅读文件的编码:
阅读器r = new InputStreamReader(new FileInputStream(myfile),StandardCharsets.UTF_8);
然后默认平台编码的值(可以使用
b
我通常会建议始终为依赖于标准语言环境的任何操作(例如字符I / O)显式指定编码。许多Java API方法默认为平台编码,我认为这是一个不好的设计,因为平台编码通常不是正确的,加上它可能突然改变(如果用户例如切换操作系统区域设置),打破你的应用程序。 >
所以只要总是说你想要的编码。
有些情况下,平台编码是正确的
注意2:
java.nio.charset.StandardCharsets
是在Java 1.7中引入的。对于较旧的Java版本,您需要将输入编码指定为String(ugh)。可能的编码列表取决于JVM,但每个JVM至少具有:
US-ASCII,ISO-8859-1,UTF-8,UTF -16BE,UTF-16LE,UTF-16。
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.
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.
这篇关于如何强制jar使用(或jvm运行)utf-8,而不是系统的默认编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!