microsoft / vscode-gradle

Manage Gradle Projects, run Gradle tasks and provide better Gradle file authoring experience in VS Code
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle
MIT License
124 stars 51 forks source link

Garbled characters occur when executing `run` tasks from extensions with Java21. #1480

Open mazeneko opened 6 months ago

mazeneko commented 6 months ago

Extension Name: vscode-gradle Extension Version: 3.13.5 OS Version: Windows11 23H2 VSCode version: 1.87.2 Java version: 21

Describe the bug Garbled characters occur when executing run tasks from extensions with Java21.

Gradle outputs in MS932 and the terminal tries to display in UTF-8.

To Reproduce reproduction tag commit of reproduction code repository

Expected behavior Gradle outputs in UTF-8 and the terminal tries to display in UTF-8.

Screenshots . /gradlew run in a terminal, no problem. image

Garbled characters occur when executing run tasks from extensions. image

cypher256 commented 6 months ago

The task can be executed correctly by setting the following in build.gradle.

application {
    mainClass = '...'
    applicationDefaultJvmArgs = ['-Dstdout.encoding=UTF-8', '-Dstderr.encoding=UTF-8']
}

However, if you run it in the terminal, you need to specify chcp 65001. You can create a terminal profile by setting the following in settings.json.

"terminal.integrated.profiles.windows": {
    "JavaSE-21": {
        "overrideName": true,
        "env": {
            "PATH": "D:\\xxx\\java\\21\\bin;${env:PATH}",
            "JAVA_HOME": "D:\\xxx\\java\\21"
        },
        "path": "cmd",
        "args": [
            "/k",
            "chcp",
            "65001"
        ]
    }
},
"terminal.integrated.defaultProfile.windows": "JavaSE-21",
"terminal.integrated.automationProfile.windows": {
    "path": "cmd"
},
"java.test.config": {
    "vmArgs": [
        "-Dstdout.encoding=UTF-8",
        "-Dstderr.encoding=UTF-8"
    ]
},

These settings can be automatically configured with the Extension Pack for Java Auto Config extension that I provide.

mazeneko commented 6 months ago

Thanks for the advice.

I'm aware that if I set Java and the terminals to UTF-8, the characters will not be garbled, and in fact I have avoiding the problem by doing so in my apps. However, this requires writing the settings for each of the various terminals used by the development team members. (And... every time you create a new project!)

I am focusing on the fact that the "Java outputs characters in UTF-8 and terminal tabs in UTF-8" behavior that worked in Java 17 is no longer working in Java 21. (Perhaps the influence of JEP400?)

This is my uncertain guess, I think need to set the Java output to UTF-8 in the extension, since the encoding of the terminal tab cannot be changed from UTF-8 when run from the extension.

cypher256 commented 6 months ago

Although it may not be a solution, Java encoding can be specified in settings.json. However, in this case, characters will be garbled when executing the command in the terminal. What has changed in JEP 400 is file.encoding.

"java.import.gradle.jvmArguments": "-Dfile.encoding=COMPAT"

image