JVM property -Dfile.encoding=UTF8 or UTF-8?

FileEncodingUtf 8Jvm

File Problem Overview


I would like to know what is the value of the Java Virtual Machine (JVM) property to set my file encoding to UTF-8.

Do I put -Dfile.encoding=UTF8 or -Dfile.encoding=UTF-8?

File Solutions


Solution 1 - File

It will be:

UTF8

See here for the definitions.

Solution 2 - File

If, running an Oracle HotSpot JDK 1.7.x, on a Linux platform where your locale suggests UTF-8 (e.g. LANG=en_US.utf8), if you don't set it on the command-line with -Dfile.encoding, the JDK will default file.encoding and the default Charset like this:

System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));

... yields:

file.encoding: UTF-8
defaultCharset: UTF-8

... suggesting the default is UTF-8 on such a platform.

Additionally, if java.nio.charset.Charset.defaultCharset() finds file.encoding not-set, it looks for java.nio.charset.Charset.forName("UTF-8"), suggesting it prefers that string, although it is well-aliased, so "UTF8" will also work fine.

If you run the same program on the same platform with java -Dfile.encoding=UTF8, without the hypen, it yields:

file.encoding: UTF8
defaultCharset: UTF-8

... noting that the default charset has been canonicalized from UTF8 to UTF-8.

Solution 3 - File

Both UTF8 and UTF-8 work for me.

Solution 4 - File

This is not a direct answer, but very useful if you don't have access to how java starts: you can set the environment variable JAVA_TOOLS_OPTIONS to -Dfile.encoding="UTF-8" and every time the jvm starts it will pick up that option.

Solution 5 - File

[INFO] BUILD SUCCESS Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8 Anyway, it works for me:)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionastrotoufView Question on Stackoverflow
Solution 1 - FileplanetjonesView Answer on Stackoverflow
Solution 2 - FilejavabrettView Answer on Stackoverflow
Solution 3 - FilePedro MadridView Answer on Stackoverflow
Solution 4 - FileMilimetricView Answer on Stackoverflow
Solution 5 - FilelivejqView Answer on Stackoverflow