使用 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屋!