0

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
���
XiaoR
  • 1
  • 1
  • Out of interest, in that cmd.exe window, what do you get from command `chcp`? Useful also to know output of `System.out.println(System.getProperty("file.encoding"));` – g00se Apr 24 '22 at 14:05
  • I got `活动代码页: 936` when I command `chcp`. file.encoding is UTF-8 – XiaoR Apr 24 '22 at 14:17
  • I found a issue about this problem https://youtrack.jetbrains.com/issue/IDEA-291006 – XiaoR Apr 24 '22 at 15:06
  • DId you try that gbk thing? – g00se Apr 24 '22 at 15:40
  • 1
    Isn’t this exactly the same question as [this one](https://stackoverflow.com/q/71685543/2711488)? – Holger Apr 26 '22 at 11:00

1 Answers1

0

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.

XiaoR
  • 1
  • 1
  • You mean you did that and it solved it? – g00se Apr 24 '22 at 15:46
  • yes, the only way to fix the issue is to use `-Dfile.encoding=GBK` which is far from perfect. – XiaoR Apr 25 '22 at 01:34
  • Interesting. I wonder if `-Dfile.encoding=x-mswin-936` would work just as well. I also wonder if the encoding of your files is *actually* UTF-8 or something else, but I'd need to see one of your actual files to find out – g00se Apr 25 '22 at 08:46
  • I tried mswin-936, it worked. My code is on `https://github.com/RShock/fumaSim/blob/main/src/test/java/%E4%B8%AD%E6%96%87Test.java` . IDEA and VSCode shows that my file is UTF-8 – XiaoR Apr 25 '22 at 14:24
  • Yes the file seems to be UTF-8. The other encoding corresponds to your Windows cmd.exe – g00se Apr 25 '22 at 14:33