Encoding errors for Java source files compiled under Windows

Source: Internet
Author: User

ImportJava.util.Arrays;P
Ublic classarrayasareference{
Public Static voidMain (string[] args)
{int[] arr =NULL;
System.out.println (arr);
arr =New int[]{1, 2, 3, 4};//Creating an Array object statically
System.out.println (arrays.tostring (arr));
Int[] prices;
Prices = new INT[5]; //dynamically creating Array objects
System.out.println (arrays.tostring (prices));
System.out.println (); //iterating through an array object using a normal loop
for (int i = 0; i < prices.length; i++) {
System.out.println ("prices[" + i + "] =" + prices[i]);
}
System.out.println (); //iterating through an array object using a Foreach Loop
for (int price:prices)
{
System.out.println ("Price:" + price);
}
}
}

Then in the Cmd.exe with Javac to compile, the following error occurred:

 Second, the analysis to resolve the occurrence of this situation, the surface reason is that the source file has GBK non-mapped characters. According to this information can be learned that the source code character encoding is not GBK, so some of the characters are not mapped to GBK encoding, so the solution is very obvious, that is, the source code encoding format is converted to GBK encoded. Specific in the notepad++ is also very simple: Check the target source code file tab, click "Format", "to ANSI encoding format", save, and then compile, this time the compilation can be passed.This problem can be encoded by the source code to GBK encoding solution , but if I do not want to turn, is to use the source code to compile the original encoding how to do it? then you have to use the-encoding parameter to specify the encoding format of the source code at compile time. For example, in this example, through the Notepad Format menu can see the source code of the original encoding format is Utf-8, then you can compile: javac-encoding utf-8 Arrayasareference.java, can also be successfully compiled through. In short, if the coding problem occurs in the compilation phase, the more general solution is to find out the encoding format of the source program, through the-encoding parameter to specify the encoding format of the source program, it will certainly be compiled through. Third, the problem extension source code format why would cause a compilation error? This problem involves JAVAC encoding the source code format processing method.

At compile time, if you do not specify the encoding format of the Java source program with the-encoding parameter, Javac.exe believes that the encoding format of the source code is the default encoding format of the operating system. As a result, the source code is processed according to the default encoding format of the operating system. Above because the default encoding format under Windows is GBK, Compile time without javac will be arrayasareference.java as GBK encoded files, but in fact Arrayasareference.java is utf-8 format, so the conversion error occurred.

Javac Compile the source code is the time to firstThe encoding format of the source program is converted toJavaInternal use ofUnicode format, and then load the Unicode format source program into memory. Afterwards compile, compile, build .class class file, at this time .class file is also the Span style= "font-family:arial, Sans-serif;" lang= "en-US" >unicode coded, it was put in memory, and finally Span style= "font-family:arial, Sans-serif;" lang= "en-US" >javac unicode coded class file saved to the operating system form .class file.

Therefore, the coding problem only appears in the compilation phase, and at run time with the Java command to execute the class file, there will never be a coding problem, because the compiled class file must be Unicode encoding.

Encoding errors for Java source files compiled under Windows

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.