I'm a Chinese programmer. Today I try to upgrade my project JDK version from 16 to 18.(also upgrade IDEA2021 to 2022) But I came across a problem, IDEA console print garbled text instead of Chinese. Here is the code and console output. problem image
I tried many ways to solve it but I failed, then I reran it in CMD and github CI, then this bug disappeared. CMD image
When I rollbacked my JDK to 16, this bug also disappeared, it seems that this bug only happens in JDK18 and IDEA console, how can I solve it?
Addition: My unit test also broken. bug image
Addition2: I tried different chcp, it outputed correctly. 65001 936
I thought is a problem about JEP400,so I ran these code
System.out.println("Default charset : " + Charset.defaultCharset());
System.out.println("file.encoding : " + System.getProperty("file.encoding"));
System.out.println("native.encoding : " + System.getProperty("native.encoding"));
System.out.println("你好");
on JDK16, it printed:
Default charset : UTF-8
file.encoding : UTF-8
native.encoding : null
你好
on JDK17, it printed
Default charset : UTF-8
file.encoding : UTF-8
native.encoding : GBK
你好
on JDK18, it printed
Default charset : UTF-8
file.encoding : UTF-8
native.encoding : GBK
���
According to https://youtrack.jetbrains.com/issue/IDEA-291006 I added
-Dfile.encoding=gbk
in VM option and solved it. I think it's a bug of IDEA.