Hi everyone, welcome to the November edition of the Visual Studio Code Java update! In this end-of-year post, we are going to look at several new improvements related to fundamental Java development experience as well as our updates related to encoding issues.
Inner-loop dev experience directly impacts developer’s day-to-day productivity and this area will always be our top focus. In November, we have made several improvements in this area:
Project Management – Remove .project metadata files
If you are doing Java development using the Extension Pack for Java, we have good news for you – Visual Studio Code no longer generates those hidden “.project” metadata files in the project path when you import a new Java project! This is an issue that has been there for over three years, and our fix was delivered in November. If you are interested in finding out how we solved it, please visit this dedicated blog post.
Testing – Navigating between tests and corresponding test subjects
In the November release, we added a new feature that allows the developer to navigate between test and corresponding test subjects. This feature may come in handy when writing unit test cases
Code Actions – Generate constructors and override/implement methods
As mentioned in previous blog post, we are constantly making common code actions more visible and easier to use. In the latest release, developers can now use the “lightbulb icon” next to the Java class to conveniently generate constructors or override/implement methods! Here is a quick demo on how to do this:
Dealing with encoding issues
It is quite common that developer nowadays deal with various languages and run into some kind of encoding issues. We’ve been hearing this kind of feedback from our users, so we wanted to share our latest finding and suggestions in this blog post.
Background
Computers can only understand the binary data such as 0 and 1, and it uses charset to encode/decode the data into real-world characters. When two processes interact with each other for I/O, they have to use the compatible charset for encoding and decoding, otherwise garbled characters might appear. MacOS and Linux use UTF-8 everywhere so encoding is not a problem for them. For Windows, however, the default charset is not UTF-8 and is platform-dependent, which can lead to inconsistent encoding between different tools.
Common Problems
Below are the typical encoding problems when running a Java program on Windows terminal.
- The file or directory name contains Unicode characters, Java launcher cannot find the corresponding classpath or main class well.
中文目录 ├── Hello.class └── Hello.java
C:\Test>java -cp 中文目录 Hello Error: Could not find or load main class Hello
- The string literals with Unicode characters appear garbled when printed to the terminal.
Exercises ├── 练习.class └── 练习.java
C:\Test>java -cp ./Exercises 练习 Error: Could not find or load main class ?? Caused by: java.lang.ClassNotFoundException: ??
- Garbled characters when Java program interacts with terminal for I/O
public class Hello { public static void main(String[] args) { System.out.println("你好!"); } }
C:\Test>chcp 65001 C:\Test>java -cp ./Exercises Hello ??! C:\Test>java -Dfile.encoding=UTF-8 -cp ./Exercises Hello 你好!
- The program needs to read Unicode characters from stdin, and print Unicode characters to stdout.
import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(scanner.nextLine()); } }
C:\Test>chcp 65001 C:\Test>java -Dfile.encoding=UTF-8 -cp ./Exercises Hello 你好 ��
Our investigation and advice to handle these issues
Previously, to mitigate the encoding issue, we added some workarounds in Java Debugger side to force to use UTF-8 in our toolchain. For example, adding a launcher.bat to force the terminal’s code page to be 65001, and set the default “file.encoding” property to be “UTF-8”. But it turns out they don’t solve the encoding problem systematically, and introduce some additional side effects as well (see #756, microsoft/vscode-java-debug#622, microsoft/vscode-java-debug#646).
After more investigation into the issue, we found that the workaround we added seemed unnecessary. The user only needs to set windows system locale to the language they want, then the JVM and terminal will automatically change to an encoding that is compatible with your system locale. This is also suggested by the official Java documentation (https://www.java.com/en/download/help/locale.html).
The following screenshot shows how to change the system locale in Windows. for example, if you want to use a terminal to enter Chinese characters into a Java program, you can set the Windows system locale to Chinese. The default Java charset will be "GBK"
and the cmd codepage will be "936"
, which will support Chinese characters nicely.
Here’s the detailed documentation on how to deal with the encoding issues.
End of year update
We are almost at the end of 2021 and for the past 12 months, we’ve been working hard to provide better Java development experience on Visual Studio Code. For 2022, there will be lots of exciting things coming for Java support on Visual Studio Code, so please stay tuned for future updates. As always, we are grateful for support from the community and we wish everyone a wonderful Christmas and happy new year!
Feedback and suggestions
Please don’t hesitate to try our product! Your feedback and suggestions are very important to us and will help shape our product in future. There are several ways to leave us feedback
- Leave your comment on this blog post
- Open an issue on our GitHub Issues page
Resources
Here is a list of links that are helpful to learn Java on Visual Studio Code.
- Learn more about Java on Visual Studio Code.
0 comments