Skip to content

fix(run): 启动和调试 Java 程序时默认使用 UTF-8 编码 - #21

Draft
AACS111 wants to merge 1 commit into
Lithe-IDEA:mainfrom
AACS111:fix/windows-utf8-launch
Draft

AACS111 wants to merge 1 commit into
Lithe-IDEA:mainfrom
AACS111:fix/windows-utf8-launch

Conversation

@AACS111

@AACS111 AACS111 commented Nov 17, 2026

Copy link
Copy Markdown

关联 Issue / Linked issue

无对应 Issue。问题在 Lithe 0.8.1 + Java Support 2.0.1 上复现,详见下文。

类型 / Type

fix:缺陷修复

背景 / Why

在中文 Windows 上用 Java Support 运行或调试 Spring Boot 应用(Java 17)时,应用启动失败:

org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'serve.system' in value "${serve.system}"

控制台中文 banner 和日志也是乱码。同一项目在 IntelliJ IDEA 中正常启动。

根因:Java 18 之前,JVM 用平台字符集读取文本,中文 Windows 上是 GBK。插件启动 JVM 时没有指定编码,Spring 从配置中心拉到的 UTF-8 YAML 被按 GBK 解析失败,配置缺失导致启动失败。IntelliJ IDEA 在启动时会固定 -Dfile.encoding=UTF-8,所以那边正常。Lithe 的运行窗口按 UTF-8 解码进程输出,控制台流也需要固定 UTF-8。

改动 / Changes

  • backend/src/run.rs
    • 新增 with_utf8_encoding:在每次运行、调试和测试启动的参数最前面加入 -Dfile.encoding=UTF-8、-Dsun.stdout.encoding=UTF-8、-Dsun.stderr.encoding=UTF-8。
    • 启动参数里已经设置的同名属性不再重复添加,保留用户的选择。
    • 在 start 中对主类和测试两种启动统一应用;调试时 JDWP agent 仍排在最前。
    • 新增两个单元测试:参数顺序在主类之前;已设置的编码属性被保留。
  • 对 Java 18 及以上版本这些值与默认一致,不改变行为;不限于 Windows,因为宿主运行窗口在所有平台都按 UTF-8 解码输出。

验证 / Verification

命令 平台 结果
cargo test -p lithe-java-backend --lib run:: Windows 11 x64 21 passed(含 2 个新测试)
同一补丁应用到 v2.0.1 并构建 lithe-java-backend,替换本机 2.0.1 插件后端 Windows 11 x64 构建成功;启动 Spring 应用的端到端验证待补

未能执行:端到端验证(用修复后的后端启动 Spring Boot 应用、确认不再出现 MalformedInputException)还没有完成,完成后在本 PR 补充结果。macOS / Linux 未运行。

🤖 Generated with Claude Code

Java 17 及更早版本在中文 Windows 上按 GBK 读取文本,Spring 应用解析
UTF-8 的 YAML 配置时抛出 MalformedInputException 而无法启动,控制台
中文也是乱码;IntelliJ IDEA 启动时会固定 UTF-8,所以同一项目在 IDEA
里正常。

每次运行、调试和测试启动都在参数最前面加入 -Dfile.encoding=UTF-8、
-Dsun.stdout.encoding=UTF-8 和 -Dsun.stderr.encoding=UTF-8,启动参数里
已经设置的属性不再重复添加。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant