The usage of javac encoding is explained in detail

  • 2020-05-24 05:33:02
  • OfStack

Yesterday, a student who had just learned java sent a program to me, saying that he would compile it to death. However, he always reported the coding problem. He tried 1 time by himself, but there was also a problem.

When we save an Java source file, we save it with the operating system's default character encoding (Windows xp default character set is GBK). When the javac command is called, the source file will be encoded once. If no character set is specified, the source file will be converted from the operating system default character set to the Java internal default unicode character set, and then the source file will be compiled into class file and saved to the hard disk in unicode encoding form.

After carefully checking 1 error, I found that it was the problem of file encoding. His file was encoded with UTF-8, so -encoding was added and compiled successfully ~ ~


public class Test {
  public static void main(String[] args) {
    System.out.println(" Hello! ");
  }
}

D:/>javac Test.java

Test.java:3:  Warning: code  GBK  The unmapable character 
    System.out.println(" Huan � ソ froe ?);
                ^
Test.java:3:  An unended string literal 
    System.out.println(" Huan � ソ froe ?);
              ^
Test.java:3:  Need to be  ';'
    System.out.println(" Huan � ソ froe ?);
                  ^
Test.java:5:  The end of the file has been reached when parsing 
}

D:/>javac -encoding utf-8 Test.java

jdk at compile time, if not - encoding parameter specifies java source program coding format, then javac first secure operating system default encoding format, i.e. during the compilation of java program, if they do not specify the source program files encoding format, jdk first secure operating system, the default encoding format is xp GBK, then jdk java source program from the internal default encoding format into java unicode format in memory. Then, javac compiles the converted unicode file into a.class file. At this point, the.class file is unicode encoded and temporarily stored in memory. Then, jdk saves the unicode compiled class file to the operating system to form a.class file. The resulting.class file is a class file whose contents are saved in unicode encoding format, which contains the Chinese strings from the source program internally, only at this point it has been converted to unicode by the installation system encoding format.

1 generally, I prefer to use UTF-8, which can run normally under windows and linux...


Related articles: