From 13ead74bc09717efbf5ff771dc9a4a1d014b9862 Mon Sep 17 00:00:00 2001 From: zhengfuping Date: Tue, 17 Nov 2026 10:45:41 +0900 Subject: [PATCH] =?UTF-8?q?fix(run):=20=E5=90=AF=E5=8A=A8=E5=92=8C?= =?UTF-8?q?=E8=B0=83=E8=AF=95=20Java=20=E7=A8=8B=E5=BA=8F=E6=97=B6?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8=20UTF-8=20=E7=BC=96?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- backend/src/run.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/backend/src/run.rs b/backend/src/run.rs index 12ad25a..f5bccd8 100644 --- a/backend/src/run.rs +++ b/backend/src/run.rs @@ -837,6 +837,7 @@ fn start(session: &RunSession, target: &Target, context: &Context) -> Result(mutex: &Mutex) -> MutexGuard<'_, T> { mutex.lock().unwrap_or_else(PoisonError::into_inner) } +/// The text encodings every launch fixes to UTF-8, as IntelliJ IDEA does. +/// +/// Before Java 18 the JVM reads files with the platform charset, which on a +/// Chinese Windows is GBK: a Spring application then fails to parse its UTF-8 +/// YAML (`MalformedInputException`) although it starts from IDEA. The host's +/// Run window decodes the process output as UTF-8, so the console streams are +/// fixed as well. A property the launch already sets is left alone. +const UTF8_ENCODING_PROPERTIES: [&str; 3] = + ["file.encoding", "sun.stdout.encoding", "sun.stderr.encoding"]; + +/// Puts the UTF-8 encoding properties ahead of the launch's own arguments, +/// skipping any the arguments already define. +fn with_utf8_encoding(arguments: Vec) -> Vec { + let mut prepared: Vec = UTF8_ENCODING_PROPERTIES + .iter() + .filter(|property| { + let prefix = format!("-D{property}="); + !arguments.iter().any(|argument| argument.starts_with(&prefix)) + }) + .map(|property| format!("-D{property}=UTF-8")) + .collect(); + prepared.extend(arguments); + prepared +} + #[cfg(test)] mod tests { use super::*; use lithe_java_domain::lsp::languages::JavaRunMarkerStatus; + #[test] + fn launches_read_and_print_text_as_utf8_before_the_main_class() { + let arguments = with_utf8_encoding(vec![ + "-cp".to_owned(), + "classes".to_owned(), + "example.Main".to_owned(), + ]); + assert_eq!( + arguments, + [ + "-Dfile.encoding=UTF-8", + "-Dsun.stdout.encoding=UTF-8", + "-Dsun.stderr.encoding=UTF-8", + "-cp", + "classes", + "example.Main", + ] + ); + } + + #[test] + fn an_encoding_the_launch_already_sets_is_kept() { + let arguments = with_utf8_encoding(vec![ + "-Dfile.encoding=GBK".to_owned(), + "example.Main".to_owned(), + ]); + assert_eq!( + arguments, + [ + "-Dsun.stdout.encoding=UTF-8", + "-Dsun.stderr.encoding=UTF-8", + "-Dfile.encoding=GBK", + "example.Main", + ] + ); + } + #[test] fn a_deferred_query_retries_at_a_work_boundary_in_either_order() { let mut retry = ActionQueryRetry::default();