Search code examples
javagradlecharacter-encodingcodepages

why System.out.charset() not equals to stdout.encoding


My operating system is Windows 10, and the system's default code page is GBK. The JDK version is 21, and the Gradle version is 8.5. I use chcp 1252 to change the console encoding. In order to check the System.out.charset of the gradle daemon process, I use System.out.print(System.out.charset()) in build.gradle,it is “utf-8”, while the VM property stdout.encoding of the Gradle daemon process is 1252. If I run a class file that outputs System.out.charset using the java command, the output of System.out.charset and stdout.encoding are both 1252. Why does the Gradle daemon output not equal stdout.encoding?

what i have tried:At first, I thought it was because stdout.encoding does not support the character set I specified. However, running a class file that outputs System.out.charset directly using the java command shows that the character set I specified is supported. I expected that System.out.charset should equal stdout.encoding. Please forgive me for my poor English.


Solution

  • Eventually found in gradle's source code, gradle modifies the output stream using System.setOut, so System.props "stdout.encoding" does not match System.out.charset(). In addition, gradle also modifies the "stdout.encoding" in Sytem.props, resulting in an inconsistency with the console encoding.