英文:
VS Code Java System Output Encoding
问题
我在 Visual Studio Code 中遇到了 Java 系统输出编码的问题。
我的程序非常简单:
public class Main {
public static void main(final String[] args) {
System.out.println("Příliš žluťoučký kůň úpěl ďábelské ódy");
}
}
但输出看起来像这样:
Příliš žluťoučký kůň úpěl ďábelské ódy�ábelské ódy�dy
我的文件使用 UTF-8 编码,并且使用 -Dfile.encoding=UTF-8
标志进行编译。我在 Visual Studio Code 中使用了由 Microsoft 提供的官方 Java 扩展套件。
英文:
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.
答案1
得分: 0
你已经安装了“Java Extension Pack”,并且提到了“-Dfile.encoding=UTF-8”,因此你是通过“Debugger for Java”扩展进行调试。这导致了我想要重现你的问题时出现了失败。
我尝试过修改终端的编码,通过task.json,或者在settings.json中添加“terminal.integrated.shellArgs.windows”:[“/K”,“chcp 65001”],但它们都不起作用。
当然,这是因为“Debugger for Java”扩展将打开一个新的终端来执行命令,上述的设置不会影响该终端。
所以,这一次我可以重现你的问题,因为我打开了“launcher.bat”文件,它在命令中显示,并且修改了其中的代码 -> “chcp.com 65001 > NUL”。chcp.com的其他任何数字都会导致你的问题。
因此,也许这可以为你提供一些启发。
英文:
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.
答案2
得分: 0
以下是翻译好的内容:
vscode与您原始的'javac,java'命令不同,因为vscode依赖于扩展来执行这些命令。
通过"Language Support for Java(TM) by Red Hat"扩展编译'.java'文件为'.class'文件,它自定义添加了'-encoding utf8'参数。
通过"Debugger for Java"扩展,更改终端的'chcp'为'65001',并添加'-Dfile.encoding=UTF-8',以确保JVM以'utf8'编码启动。
您可以通过添加环境变量来实现相同的效果:
"JDK_JAVAC_OPTIONS":"-encoding utf8",
"JDK_JAVA_OPTIONS":"-Dfile.encoding=UTF-8",
以及'chcp 65001'来临时更改终端编码。
这非常复杂和奇怪,因为我无法确定'java','javac'的默认编码是什么,我尝试了很多但仍然困惑,虽然我搜索了很多,但无法获得足够的信息。但所有内容都包含在这三个变量中,我希望这些能为您提供一些有用的信息,我太累了,抱歉…
英文:
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...
答案3
得分: 0
以下是翻译好的部分:
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,
英文:
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,
答案4
得分: 0
我遇到了类似的问题。我的环境有点复杂。
我同时使用了 'Code-Runner' 和 'Java-extenstion Pack'。这似乎让我更难解决这个问题。
经过多次尝试,我发现了以下几点。
-
当我使用运行图标(位于右上方)时,'code-runner' 会在“OUTPUT”中编译并运行我的代码,显示乱码的韩文字符。
-
当我使用“Run > Debugging”或“Run > Run Without Debugging”时,'Java extension Pack' 会在“TERMINAL”中编译并运行我的代码,正确显示韩文字符。
以下是我的最终设置。
A. 工作区中的 settings.json
- 在 'code-runner.executoMap' 中插入 -encoding utf-8
....
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",
....
"sml": "cd $dir && sml $fileName"
}
B. 用户设置中的 settings.json
- 添加
"java.jdt.ls.vmargs": "-Dfile.encoding=utf-8"
- 添加
"code-runner.runInTerminal": true
设置后,code-runner 在“TERMINAL”中能够正确显示韩文字符(而不是“OUTPUT”),'java extension pack' 也是如此。
英文:
I faced the similar issue. My environment is a little bit complicated.
I use both of 'Code-Runner' and 'Java-extenstion Pack.' That seems to make it harder for me to resolve the issue.
After several trying, I found out below things.
-
When I use run icon (in upper-right side), 'code-runner' compine and run my code in "OUTPUT", which shows broken Korean characters.
-
When I use "Run > Debugging" or "Run > Run Without Debugging", 'Java extension Pack' compile and run my code in "TERMINAL", which shows Korean characters properly.
Below is my final settings.
A. settings.json in workspace
- Insert -encoding utf-8 in 'code-runner.executoMap'
....
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",
.....
"sml": "cd $dir && sml $fileName"
}
B. settings.json in User
- Add
"java.jdt.ls.vmargs": "-Dfile.encoding=utf-8"
- Add
"code-runner.runInTerminal": true
After setting, code-runner prints Korean characters well in "TERMINAL"(not "OUTPUT"), and 'java extension pack' also does.
专注分享java语言的经验与见解,让所有开发者获益!
评论