当前位置:   article > 正文

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

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

解决方案

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.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/850550
推荐阅读
相关标签
  

闽ICP备14008679号