Java garbled problem-dfile.encoding=utf-8

Source: Internet
Author: User

http://blog.csdn.net/telnetor/article/details/5555361

Problem Description:
The program involves internationalization problems, HttpClient grabbed back the data messy garbled, after a few times after the code in MyEclipse can get the normal code source (accurately said to be able to show a large part of the content is still garbled), But porting the program to eclipse has been garbled (the porting effort has tried several forms: 1, the program is exported from MyEclipse, then imported from Eclipse, and 2, the Eclipse Workspace is switched to the MyEclipse workspace.) Then set the project code to UTF-8). If the MyEclipse project is packaged with Fatjar as an executable jar package and then runs under Windows, it is garbled. It is normal to be in a Linux environment.
In Linux to perform timed tasks when the garbled (the first time the system is the default environment, the installation of the system does not modify the environment settings, this time the program runs well, when the installation of some language-related packages, the program is abnormal).
Problem Analysis:
Engineering codes in MyEclipse and Eclipse are all Utf-8
Windows environment Code GBK, Linux environment does not appear exception when the default encoding is UTF8, exception while the system environment display Utf-8, but because of the installation of some of the system environment of the package, the encoding is utf-8 a little doubt
The final encoding of the conversion in the program Utf-8
Suspect object is the default encoding problem for program run environment
Unexpected discovery of JVM when using Java VIRTUALVM parser memory, thread usage The parameters in arguments are different for running programs under Eclipse and MyEclipse: Eclipse has only the parameters of-xmx1024m and the program under MyEclipse one more:-dfile.encoding=utf-8
When you start the program with the-dfile.encoding=utf-8 parameter, program Eclipse garbled disappears, Linux under the scheduled task execution program is not garbled.

-dfile.encoding Explanation:
Enter Java on the command line, and the-D description appears in the hints given:
-d=
Set a system property
-D needs to be followed by a key-value pair to set a system property
For-dfile.encoding=utf-8, setting the system property file.encoding to UTF-8
So what does file.encoding mean? literal meaning for file encoding.
Search Java source code, can only find 4 files containing file.encoding files, that is, only four files called file.encoding this property.
In the Charset.java in the Java.nio.charset package. The meaning of this passage is very clear, simply said that the default character set is determined when the Java Virtual machine is started, depending on the region of the operating system where the Java virtual machine resides and the character set.
As you can see in the code, the default character set is obtained from the File.encoding property. Personal feeling is one of the most important factors. The following three can be seen.
/**
* Returns The default charset of this Java virtual machine.
*
*

The default charset is determined during Virtual-machine startup and
* Typically depends upon the locale and charset of the underlying
* Operating system.
*
* @return A CharSet object for the default charset
*
* @since 1.5
*/
public static Charset Defaultcharset () {
if (Defaultcharset = = null) {
Synchronized (Charset.class) {
Java.security.PrivilegedAction PA =
New Getpropertyaction ("file.encoding");
String CSN = (string) accesscontroller.doprivileged (PA);
Charset cs = lookup (CSN);
if (cs! = null)
Defaultcharset = CS;
Else
Defaultcharset = forname ("UTF-8");
}
}
return defaultcharset;
}

Inside the static block in the Urlencoder.java in the java.net package:
Dfltencname = (String) accesscontroller.doprivileged (
New Getpropertyaction ("file.encoding")
);
The Docflavor.java in the Javax.print package
static {
Hostencoding =
(String) java.security.AccessController.doPrivileged (
New Sun.security.action.GetPropertyAction ("file.encoding"));
}
The encodings in the Com.sun.org.apache.xml.internal.serializer package
Get the default system character encoding. This May
Incorrect if they passed in a writer, but right now there
Seems to is no-to-get the encoding from a writer.
encoding = System.getproperty ("file.encoding", "UTF8");

Another netizen's blog read above:
Http://yaojingguo.blogspot.com/2009/02/javas-fileencoding-property-on-windows.html
Java ' s file.encoding property on Windows platfor
The used for the default encoding in Java, all readers and writers would the default to using the. File.encoding is set to the default locale of the Windows operationg system since Java 1.4.2. System.getproperty ("file.encoding") can is used to access the property. Code such as System.setproperty ("file.encoding", "UTF-8") can is used to the change the property. However, the default encoding can be is not changed dynamically even this property can be changed. So, the conclusion is, the default encoding can ' t change after JVM starts. Java-dfile.encoding=utf-8 can used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. But I can ' t find it.

Java garbled problem-dfile.encoding=utf-8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.