错误:无法映射的字符 (0x80) 用于编码 UTF-8
error: unmappable character (0x80) for encoding UTF-8
ID: java/unmappable-character-encoding
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
根因分析
Java 源文件包含编译器指定的 UTF-8 编码中无效的字节序列,通常是由于字符串字面量或注释中存在非 UTF-8 字符(例如来自 Windows-1252 或 ISO-8859-1)。
English
The Java source file contains a byte sequence that is not valid in the UTF-8 encoding specified for the compiler, often due to a non-UTF-8 character (e.g., from Windows-1252 or ISO-8859-1) being present in a string literal or comment.
官方文档
https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#options解决方案
-
Specify the correct source encoding to javac using the -encoding flag. If the file is actually in Windows-1252, use -encoding Cp1252.
-
Convert the source file to UTF-8 using a tool like iconv or a text editor that supports encoding conversion.
-
Use native2ascii to escape the unmappable character as a Unicode escape sequence.
无效尝试
常见但无效的做法:
-
95% 失败
Changing the system locale does not affect the javac encoding; the compiler encoding must be explicitly set.
-
90% 失败
Adding -Dfile.encoding=UTF-8 to JVM arguments does not affect javac compilation encoding.
-
60% 失败
Removing the character without understanding its origin may break the intended functionality (e.g., a special symbol in a string).