简体   繁体   中英

file.encoding system property in Java giving different results in different instances of JVM

I am running Java 7 on Centos 6.6. I checked file.encoding system property in an instance of standalone tomcat server it showed UTF-8 . I check the same in an embedded tomcat it showed ANSI_X3.4-1968 . Both instances are not taking any command line arguments for file.encoding .

The mystery is why does JVM infer ANSI_X3.4-1968 in embedded tomcat only? In all other instances it correctly infers UTF-8 based on my locale settings.

Java (not Tomcat) sets file.encoding automatically if it's not given on the command line. It will use environment variables, like locale to work out an appropriate setting.

For example, my locale is en_GB.UTF-8 , so my file.encoding is resolved to UTF-8 .

The "embedded" instance may not have been given a full environment context, so has been unable to arrive at the same setting as the standalone instances.

Some more information about the embedded instance may help up shed more light.

If you always want file.encoding to be set to UTF-8 , then you must launch your JVM with this option:

-Dfile.encoding=UTF-8

Any reliance on the specific environment that is launching the JVM is not going to be reliable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM