How to force a jar to use(or the jvm that jar runs

2019-01-14 02:17发布

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.

2条回答
地球回转人心会变
2楼-- · 2019-01-14 03:06

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.

查看更多
Luminary・发光体
3楼-- · 2019-01-14 03:08

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

查看更多
登录 后发表回答