What is the default encoding of the JVM?

JavaEncodingCharacter EncodingJvm

Java Problem Overview


Is UTF-8 the default encoding in Java?
If not, how can I know which encoding is used by default?

Java Solutions


Solution 1 - Java

The default character set of the JVM is that of the system it's running on. There's no specific value for this and you shouldn't generally depend on the default encoding being any particular value.

It can be accessed at runtime via Charset.defaultCharset(), if that's any use to you, though really you should make a point of always specifying encoding explicitly when you can do so.

Solution 2 - Java

Note that you can change the default encoding of the JVM using the confusingly-named property file.encoding.

If your application is particularly sensitive to encodings (perhaps through usage of APIs implying default encodings), then you should explicitly set this on JVM startup to a consistent (known) value.

Solution 3 - Java

There are three "default" encodings:

  • file.encoding:
    System.getProperty("file.encoding")

  • java.nio.Charset:
    Charset.defaultCharset()

  • And the encoding of the InputStreamReader:
    InputStreamReader.getEncoding()

You can read more about it on this page.

Solution 4 - Java

To get default java settings just use :

java -XshowSettings 

Solution 5 - Java

I am sure that this is JVM implemenation specific, but I was able to "influence" my JVM's default file.encoding by executing:

export LC_ALL=en_US.UTF-8

(running java version 1.7.0_80 on Ubuntu 12.04)

Also, if you type "locale" from your unix console, you should see more info there.

All the credit goes to http://www.philvarner.com/2009/10/24/unicode-in-java-default-charset-part-4/

Solution 6 - Java

You can use this to print out the JVM defaults

import java.nio.charset.Charset;
import java.io.InputStreamReader;
import java.io.FileInputStream;

public class PrintCharSets {
        public static void main(String[] args) throws Exception {
                System.out.println("file.encoding=" + System.getProperty("file.encoding"));
                System.out.println("Charset.defaultCharset=" + Charset.defaultCharset());
                System.out.println("InputStreamReader.getEncoding=" + new InputStreamReader(new FileInputStream("./PrintCharSets.java")).getEncoding());
        }
}

Compile and Run

javac PrintCharSets.java && java PrintCharSets

Solution 7 - Java

It's going to be locale-dependent. Different locale, different default encoding.

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
Questionuser67722View Question on Stackoverflow
Solution 1 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 2 - JavaBrian AgnewView Answer on Stackoverflow
Solution 3 - JavamrclrchtrView Answer on Stackoverflow
Solution 4 - JavaJuwitView Answer on Stackoverflow
Solution 5 - Javamike32bView Answer on Stackoverflow
Solution 6 - JavasdcView Answer on Stackoverflow
Solution 7 - JavaskaffmanView Answer on Stackoverflow