java encoding_error ai_generated true

错误:无法映射的字符 (0x80) 用于编码 UTF-8

error: unmappable character (0x80) for encoding UTF-8

ID: java/unmappable-character-encoding

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2024-01-10首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#options

解决方案

  1. Specify the correct source encoding to javac using the -encoding flag. If the file is actually in Windows-1252, use -encoding Cp1252.
  2. Convert the source file to UTF-8 using a tool like iconv or a text editor that supports encoding conversion.
  3. Use native2ascii to escape the unmappable character as a Unicode escape sequence.

无效尝试

常见但无效的做法:

  1. 95% 失败

    Changing the system locale does not affect the javac encoding; the compiler encoding must be explicitly set.

  2. 90% 失败

    Adding -Dfile.encoding=UTF-8 to JVM arguments does not affect javac compilation encoding.

  3. 60% 失败

    Removing the character without understanding its origin may break the intended functionality (e.g., a special symbol in a string).