Java code:
// Hello world
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World! 汉字!");
}
}
output as follow:
Hello World! 姹夊瓧锛�
but java file have the default code utf-8. and Debugger for java default also utf8, why the output is abnormal character!
Works for me:

Since this is a debugger issue, I'll let @yaohaizh, @andxu and co investigate (they should be familiar with Chinese characters).
please open an issue in https://github.com/Microsoft/vscode-java-debug/issues
@fbricon your java file is utf-8? my java file is default utf-8. your files encoding?
Are you sure your ext is Debugger for Java?
Yes and yes
@xpf7622 Seems it is related to the default encoding settings of eclipse, open the project with eclipse and change the encoding for the project and the generated class will have the right encoding.
$ cd /Users/kevinfeng/Desktop/java ; /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:63788 -Dfile.encoding=UTF-8 -cp "/Users/kevinfeng/Library/Application Support/Code/User/workspaceStorage/b72b50a9aa05f002d6a626eedc811d53/redhat.java/jdt_ws/jdt.ls-java-project/bin" Hello
The output is ������
jdt compiler will use the os default encoding to compile the source code, no regarding what encode is used in vscode editor. If the user's system is Chinese, the system default encoding could be GBK and use that to compile the UTF-8 source file will cause messy string in .class.
Eclipse IDE provides UI for setting text file encoding, and the compiler will consume that automatically to keep the consistent. But in VSCode, the underlying compiler will not consume the encoding that the user saw in his editor.
Is it possible to let the jdt compiler to read the source file with the vscode editor encoding? Or at least provide a way to let the user to configure the compiler encoding?
@fbricon Given your guys are more familiar with the eclipse base code, any thoughts on this?
@testforstephen What would be the way for the server to receive the file encoding? Is it part of the settings?
Maven pom.xml file supports encoding config:
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
Gradle supports setting Java Compiler Encoding:
compileJava.options.encoding = 'UTF-8'
When importing maven or gradle project into VSCode, java language server will translate the encoding info into a file .settings/org.eclipse.core.resources.prefs with content encoding/<project>=XXX. That's no problem.
But when opening simple java files (non eclipse project files) in VSCode, there is no way to override the compiler encoding. How about adding a new User Settings like java.file.encoding to override the Java Compiler Encoding?
I think maven and gradle settings are already respected by the server. Are the standalone Java files causing the mischief?
YES, that's the point.
We can probably fall back to a default encoding from Settings if a project does not configure it by other means.
@testforstephen @andxu can any of you guys look into providing a fix?
@SummerSun Could you help fix this issue?
When importing maven or gradle project into VSCode, java language server will translate the encoding info into a file
.settings/org.eclipse.core.resources.prefswith contentencoding/<project>=XXX. That's no problem.
@testforstephen How does it work? I open a gradle project with
compileJava.options.encoding = "UTF-8" in the build.gradle file, but running the command Java: Update project configuration didn't generate the file org.eclipse.core.resources.prefs
@Fubuchi This looks like a bug of gradle eclipse plugin . See https://github.com/gradle/gradle/issues/898
@Fubuchi For gradle project, here is a possible workaround.
In the .settings/ folder, manually add a file org.eclipse.core.resources.prefs with contents below:
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
close your vscode window. then remove bin/ folder and clean the stale workspace cache.
The cache directory for different OS:
Windows : %APPDATA%\Code\User\workspaceStorage\
MacOS : $HOME/Library/Application Support/Code/User/workspaceStorage/
Linux : $HOME/.config/Code/User/workspaceStorage/
after that, reload your project to vscode.
I solved this problem.
The following configuration is added to the pom.xml file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@testforstephen
https://github.com/redhat-developer/vscode-java/issues/479#issuecomment-428488991
In the .settings/ folder, manually add a file org.eclipse.core.resources.prefs with contents below:
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/
and remove ./bin folder ,I solve the chinese character problem,thank you!
@Fubuchi For gradle project, here is a possible workaround.
In the
.settings/folder, manually add a fileorg.eclipse.core.resources.prefswith contents below:eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 encoding/<project>=UTF-8close your vscode window. then remove
bin/folder and clean the stale workspace cache.
The cache directory for different OS:Windows : %APPDATA%\Code\User\workspaceStorage\ MacOS : $HOME/Library/Application Support/Code/User/workspaceStorage/ Linux : $HOME/.config/Code/User/workspaceStorage/after that, reload your project to vscode.
Great! I solve the problem after this action.No maven or gradle project.
I hit the same issue yesterday. It is great that we have a workaround, but unfortunately the developer experience is less than ideal, when compared with IntelliJ... When can we have a proper fix? Thanks.
@zhangqi5 The issue is fixed by https://github.com/redhat-developer/vscode-java/commit/b5e63308dd3de98911fe21b56d312d3c33f54d41
You can find the latest nightly build for testing: http://download.jboss.org/jbosstools/jdt.ls/staging/java-0.39.0-1379.vsix
Make sure you invoke Java: Clean workspace ... to delete stale object/cache.
Yes, the issue cannot be reproed with the nightly build. Thanks!
Most helpful comment
@Fubuchi For gradle project, here is a possible workaround.
In the
.settings/folder, manually add a fileorg.eclipse.core.resources.prefswith contents below:close your vscode window. then remove
bin/folder and clean the stale workspace cache.The cache directory for different OS:
after that, reload your project to vscode.