VS Code Java System Output Encoding

Viewed 2008

I have a problem with encoding of Java System Output occurring only in Visual Studio Code.

My program is very simple:

public class Main {
    public static void main(final String[] args) {
        System.out.println("Příliš žluťoučký kůň úpěl ďábelské ódy");
    }
}

but the output looks like this:

Příliš žluťoučký kůň úpěl ďábelské ódy�ábelské ódy�dy

My file has the UTF-8 encoding and it's being compiled with -Dfile.encoding=UTF-8 flag. I am using the official Java Extension Pack by Microsoft in Visual Studio Code.

3 Answers

as you had installed "Java Extension Pack", and mentioned "-Dfile.encoding=UTF-8", so, you are debug through "Debugger for Java" extension. that's caused the fails of I want to recurrence your problem.

I tried to edit the encoding of terminal, through task.json, or in the settings.json adds "terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"], but both of them not works.

that's of course, as the "Debugger for Java" extension will open a new terminal to execute the commands, as the settings above will not take a infulence on that terminal.

so, I can recurrence your problem this time, as I open the "launcher.bat" file, which shows in the commands, and change the code -> "chcp.com 65001 > NUL" in it. any other number of chcp.com will caused your problem.

so, maybe, this can provide some inspirations to you.

vscode was different from your original 'javac,java' commands, as vscode depends on extensions to execute those commands.

through "Language Support for Java(TM) by Red Hat" extension compile '.java' file to '.class', it customized add '-encoding utf8' args.

through "Debugger for Java" extension to change the terminal 'chcp' to '65001', and '-Dfile.encoding=UTF-8' to make sure the JVM start with encoding 'utf8'.

you can achieve the same effects through add enviroments variable: "JDK_JAVAC_OPTIONS":"-encoding utf8", "JDK_JAVA_OPTIONS":"-Dfile.encoding=UTF-8", and 'chcp 65001' to temporary change the terminal encoding.

it's very complex and strange, as I can not sure what's the 'java', 'javac' default encoding, I had tried a lot but still confusing, as I googled a lot, but cann't get the enough informations. but everythings are included in those three variables, I hope this can provide you some useful informations, I am too tired. sorry...

for code runner extention,just add this code to user setting json:

"code-runner.executorMap":{"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",},
"code-runner.runInTerminal": true,
Related