Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.10

'javac' Using CP1252 to Process Source File

This section provides a tutorial example showing how 'javac' processing Java source code files with CP1252 encoding.

In order to test the "native2ascii" tool, I wrote the following Java source code file: HelloUtf8.java

public class HelloUtf8 {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
      System.out.println("世界你好!"); 	
   }
}

"HelloUtf8.java" contains a string of Chinese characters written in UTF-8 encoding. When I tried to compile it, I got the following warning:

C:\herong>javac HelloUtf8.java

HelloUTF8.java:4: warning: unmappable character for encoding Cp1252
      System.out.println("S+�t��S+�s�+n+?");
                                        ^
1 warning

The compiler used "Cp1252" as the default encoding to process the source file, HelloUtf8.java. The last character of my Chinese string encoded in UTF-8 can not be mapped to any CP1252 character. But the compiler did finish the compilation. The output HelloUtf8.class can still be executed in JVM:

C:\herong>java HelloUtf8

Hello world!
S+�t��S+�s�+n+?

But the last character was compiled as a question mark "?" character. To fix this problem, you need to use the "native2ascii" tool - see the next section.

Sections in This Chapter

'native2ascii' - Encoding Converter Command and Options

'javac' Using CP1252 to Process Source File

UTF-8 to \udddd Conversion with 'native2ascii -encoding'

Setting UTF-8 Encoding in PrintStream

Converting \udddd Sequences Back with "-reverse" Option

Dr. Herong Yang, updated in 2008
'javac' Using CP1252 to Process Source File