Is UTF-8 the default encoding in Java?
If not, how can I know which encoding is used by default?
-
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.– jamshidJan 18 at 20:48
7 Answers
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.
-
7If 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
-
43UTF-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).– JesperEJan 12, 2012 at 12:30 -
8This answer is correct, but for reference, on Linux it's usually "UTF-8", and on Windows it's usually "cp1252".– JeutnargJan 22, 2016 at 19:31
-
I have just experienced an linux installation that report UTF-8 from locale, but java says US-ASCII. Jan 26, 2017 at 9:02
-
1Wrong. Check
Charset.defaultCharset()
source code. It readsfile.encoding
property, otherwise uses UTF-8. Mar 28, 2018 at 12:27
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.
-
19Note 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/…– sleskeFeb 25, 2010 at 12:38
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.
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/
-
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 injava.lang.System.initProperties
based on the value ofsprops.encoding
, wheresprops
is a structure returned by the native functionGetJavaProperties()
, the implementation of which varies according to platform. In the Windows version, for example, it callsGetUserDefaultLCID()
and thenGetLocaleInfo (lcid, LOCALE_IDEFAULTANSICODEPAGE, ...)
to find the user's default ANSI code page and uses that. On Unix platforms, it parses the return ofsetlocale(LC_CTYPE, NULL)
.– JulesMay 25, 2018 at 19:01 -
... See hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/… and hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/solaris/… for details.– JulesMay 25, 2018 at 19:05
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
It's going to be locale-dependent. Different locale, different default encoding.