用BOM编译(javac)UTF8编码的Java源代码 [英] Compiling (javac) a UTF8 encoded Java source code with a BOM
问题描述
您好,谢谢您阅读我的文章.
Hello and thank you for reading my post.
我的问题如下:我想用"javac"编译一个Java源文件,该文件是用BOM编码的UTF-8(操作系统是WinXP).
My problem is the following: I want to compile a Java source file with "javac" with this file being UTF-8 encoded with a BOM (the OS is WinXP).
以下是我的工作:
1)使用记事本"创建文件,然后选择UTF-8编码
1) Create a file with "Notepad" and choose the UTF-8 encoding
dos> notepad Test.java
"File -> Save as..."
File name : Test.java
Save as type: All Files
Encoding : UTF-8
Save
2)在该文件中创建一个Java类,然后像1)中一样保存文件
2) Create a Java class in that file and saved the file like in 1)
public class Test
{
public static void main(String [] args)
{
System.out.println("This is a test.");
}
}
3)可视化文件的十六进制版本(第一行)
3) Visualize the hexadecimal version of the file (first line)
dos> xxd Test.java | head -1
0000000: efbb bf70 7562 6c69 6320 636c 6173 7320 ...public class
注意: ef bb bf 是UTF-8编码的BOM(UTF-16编码的BOM是 FE FF ).
Note: ef bb bf is the UTF-8 encoded BOM (the UTF-16 encoded BOM being FE FF).
4)尝试使用"javac"编译此代码
4) Try to compile this code with "javac"
dos> javac -encoding utf8 Test.java
Test.java:1: illegal character: \65279
?public class Test
^
1 error
注意: 65279 是BOM的十进制版本.
Note: 65279 is the decimal version of the BOM.
我的问题如下:如何使此编译工作与
My question is the following: how can I make this compiling work with:
- 将其保留为UTF-8编码
- 并保留BOM?
感谢您的帮助和最诚挚的问候.
Thank you for helping and best regards.
莉亚
推荐答案
修剪BOM表,然后使用javac -encoding utf8 x.java
Trim the BOM and then use javac -encoding utf8 x.java
这篇关于用BOM编译(javac)UTF8编码的Java源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!