81

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?

javabrett
  • 7,020
  • 4
  • 51
  • 73
twopheek
  • 1,035
  • 2
  • 8
  • 10

5 Answers5

64

It will be:

UTF8

See here for the definitions.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
planetjones
  • 12,469
  • 5
  • 50
  • 51
  • 1
    in fact, I have already seen this page. But, I didn't know which of the "Canonical Name for java.nio API" or the "Canonical Name for java.io and java.lang API" I must use. – twopheek May 17 '11 at 14:29
  • 13
    Both work but `UTF-8` is preferred over `UTF8` - the only standard format. – Tiny Feb 12 '16 at 05:39
  • When using Spring Boot try "UTF-8". It worked in my case. – Danilo Gomes Feb 26 '16 at 13:39
  • See my answer for why I favour `UTF-8`, since it pops up for all kinds of defaults in the JDK. – javabrett Sep 29 '16 at 00:23
  • 7
    This answer says `UTF8` without hyphen - the comments to this answer, however, advise to use the hyphanated form `UTF-8`. Not very helpful. – Bernhard Döbler Mar 24 '19 at 13:48
  • 1
    I see nothing in your link to justify your assertion that "_It will be UTF8_". You could just as easily have stated "_It will be UTF-8_" based on that documentation. Can you explain why you specified "_UTF8_"? – skomisa Jan 05 '20 at 03:49
  • I've tested both UTF8 and UTF-8 and it appears to give me the same result in my build. – lkisac Jul 29 '20 at 18:06
  • 1
    `UTF8`, `UTF-16`, `UTF_32`... – Artfaith Dec 14 '21 at 02:16
25

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.

javabrett
  • 7,020
  • 4
  • 51
  • 73
11

Both UTF8 and UTF-8 work for me.

Pedro Madrid
  • 1,887
  • 1
  • 20
  • 32
8

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.

Milimetric
  • 13,411
  • 4
  • 44
  • 56
8

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

livejq
  • 534
  • 5
  • 4