Java garbled problem-dfile. Encoding = UTF-8

Source: Internet
Author: User

Problem description:
the Program involves international issues. The data captured by httpclient is messy, after several times of encoding, you can get the source code of the normal encoding in myeclipse (accurately speaking, it can display a large part, and a small part of the content is still garbled ), however, garbled code occurs when porting the program to the previous program under eclipse (the porting has tried the following forms: 1. The program is exported from myeclipse and then imported from eclipse; 2. Switch the eclipse workspace to the myeclipse workspace. Set the project encoding to UTF-8 ). If you use fatjar to package the myeclipse project as an executable jar package and run it in windows, garbled characters are still involved. But in Linux, it is normal.
garbled characters occur when scheduled tasks are executed on Linux (the system is the default environment at the earliest time, and the environment settings are not modified by the installed system. At this time, the program runs well, the program is abnormal when some packages involving languages are installed ).
Problem Analysis:
the engineering codes in myeclipse and eclipse are UTF-8.
the Windows encoding GBK and Linux encoding are UTF-8 by default when no exception occurs, however, after installing some packages related to the system environment, I have some doubts about UTF-8 encoding.
the final UTF-8 encoding of the conversion in the Program
I suspect that the object is the default encoding in the program running environment.
I have analyzed it using the Java built-in tool Java virtualvm. program memory, thread usage, it is unexpectedly discovered that the parameters in JVM arguments are different for running programs in eclipse and myeclipse: the parameters in eclipse are only-xmx1024m, and the program in myeclipse has one more parameter:-dfile. encoding = UTF-8
added-dfile when the program was started. encoding = UTF-8 parameters, the program eclipse garbled disappears, Linux scheduled task execution program is not garbled

-Dfile. Encoding explanation:
Enter Java in the command line, and the-D description will appear in the prompt:
-D =
Set a system property
-D is followed by a key-value pair to set a system attribute.
For-dfile. Encoding = UTF-8, it is to set the system property file. encoding to UTF-8
So what does file. Encoding mean? The literal meaning is the file encoding.
Search for the Java source code. Only four files containing file. encoding can be found. That is to say, only four files call the file. Encoding attribute.
In charset. Java in the Java. NiO. charset package. The meaning of this passage is very clear. Simply put, the default character set is determined at the startup of the Java Virtual Machine and depends on the operating system region and Character Set of the Java Virtual Machine.
CodeThe default character set is obtained from the file. Encoding attribute. Personally, this is the most important factor. Let's take a look at the three below.
/**
* 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 (defacharcharset = 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;
}

In the static block of urlencoder. Java in the java.net package:
Dfltencname = (string) accesscontroller. doprivileged (
New getpropertyaction ("file. encoding ")
);
Docflavor. Java in the javax. Print package
Static {
Hostencoding =
(String) Java. Security. accesscontroller. doprivileged (
New sun. Security. Action. getpropertyaction ("file. encoding "));
}
Encodings in the com.sun.org. Apache. xml. Internal. serializer package
// Get the default system character encoding. This may be
// Incorrect if they passed in a writer, but right now there
// Seems to be no way to get the encoding from a writer.
Encoding = system. getproperty ("file. encoding", "utf8 ");

Another netizen's blog saw the following:
Http://yaojingguo.blogspot.com/2009/02/javas-fileencoding-property-on-windows.html
Java's file. Encoding property on Windows platfor
This property is used for the default encoding in Java, all readers and writers wocould default to using this property. file. encoding is set to the default locale of Windows operationg system since Java 1.4.2. system. getproperty ("file. encoding ") can be used to access this property. code such as system. setproperty ("file. encoding "," UTF-8 ") can be used to change this property. however, the default encoding can be not changed dynamically even this property can be changed. so the conclusion is that the default encoding can't change after JVM starts. java-dfile. encoding = UTF-8 can be 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.

Related Article

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.