71

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

1
  • If a docker image does not have ENV LANG=en_US.UTF-8 you can see very confusing behavior where "locale" is POSIX on startup but if you exec into the container it shows UTF-8. Best not to rely on file.encoding, always specify the encoding explicitly when creating a stream.
    – jamshid
    Jan 18 at 20:48

7 Answers 7

96

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.

7
  • 7
    If you are correct I find it a bit strange java.sun.com/javase/technologies/core/basic/intl/… says that it's always UTF-16. Nov 3, 2011 at 16:11
  • 43
    UTF-16 is how text is represented internally in the JVM. The default encoding determines how the JVM interprets bytes read from files (using FileReader, for example).
    – JesperE
    Jan 12, 2012 at 12:30
  • 8
    This answer is correct, but for reference, on Linux it's usually "UTF-8", and on Windows it's usually "cp1252".
    – Jeutnarg
    Jan 22, 2016 at 19:31
  • I have just experienced an linux installation that report UTF-8 from locale, but java says US-ASCII.
    – Gunslinger
    Jan 26, 2017 at 9:02
  • 1
    Wrong. Check Charset.defaultCharset() source code. It reads file.encoding property, otherwise uses UTF-8. Mar 28, 2018 at 12:27
34

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.

1
  • 19
    Note that file.encoding must be specified on JVM startup (i.e. as cmdline parameter -Dfile.encoding or via JAVA_TOOLS_OPTIONS); you can set it at runtime, but it will not matter. See stackoverflow.com/questions/361975/…
    – sleske
    Feb 25, 2010 at 12:38
33

To get default java settings just use :

java -XshowSettings 
31

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.

0
14

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/

3
  • How did you check it? I can't find a proof Java pays any attention to the encoding in the locale string. Only from file.encoding property. Mar 28, 2018 at 13:19
  • 3
    @ArtemNovikov - yes, but what is the default value of file.encoding? It's initialised in java.lang.System.initProperties based on the value of sprops.encoding, where sprops is a structure returned by the native function GetJavaProperties(), the implementation of which varies according to platform. In the Windows version, for example, it calls GetUserDefaultLCID() and then GetLocaleInfo (lcid, LOCALE_IDEFAULTANSICODEPAGE, ...) to find the user's default ANSI code page and uses that. On Unix platforms, it parses the return of setlocale(LC_CTYPE, NULL).
    – Jules
    May 25, 2018 at 19:01
12

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
7

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

1
  • So it depends on the encoding the host operating system has? May 21, 2015 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.