如何编译编码为“UTF-8"的java源文件? [英] How to compile a java source file which is encoded as "UTF-8"?
问题描述
我保存了我的 Java 源文件,指定它的编码类型为 UTF-8(使用记事本,默认情况下记事本的编码类型是 ANSI),然后我尝试使用以下方法编译它:
I saved my Java source file specifying it's encoding type as UTF-8 (using Notepad, by default Notepad's encoding type is ANSI) and then I tried to compile it using:
javac -encoding "UTF-8" One.java
但它给出了一个错误信息"
but it gave an error message"
One.java:1: illegal character: 65279
?public class One {
^
1 error
有没有其他办法,我可以编译这个?
Is there any other way, I can compile this?
这是来源:
public class One {
public static void main( String[] args ){
System.out.println("HI");
}
}
推荐答案
您的文件正在被读取为 UTF-8,否则永远不会出现值为65279"的字符.javac
期望您的源代码采用平台默认编码,根据 javac
文档:
Your file is being read as UTF-8, otherwise a character with value "65279" could never appear. javac
expects your source code to be in the platform default encoding, according to the javac
documentation:
如果未指定-encoding,则使用平台默认转换器.
If -encoding is not specified, the platform default converter is used.
十进制 65279 是十六进制 FEFF,即 Unicode 字节顺序标记 (BOM).在 UTF-8 中没有必要,因为 UTF-8 始终编码为八位字节流并且没有字节序问题.
Decimal 65279 is hex FEFF, which is the Unicode Byte Order Mark (BOM). It's unnecessary in UTF-8, because UTF-8 is always encoded as an octet stream and doesn't have endianness issues.
即使不需要,记事本也喜欢保留在 BOM 中,但有些程序不喜欢找到它们.正如其他人指出的那样,记事本不是一个很好的文本编辑器.切换到不同的文本编辑器几乎肯定会解决您的问题.
Notepad likes to stick in BOMs even when they're not necessary, but some programs don't like finding them. As others have pointed out, Notepad is not a very good text editor. Switching to a different text editor will almost certainly solve your problem.
这篇关于如何编译编码为“UTF-8"的java源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!