Hi everyone, and welcome to the November edition of the Visual Studio Code Java update! We will share some of the latest features related to Java basic development and some solutions to coding problems.
Basic development-related functions will directly affect the daily work efficiency of developers, and improving the user experience in this area will always be our focus. In the November update, we made several improvements in this area:
Test-Jump between test and test subject
In the November version, we added a new feature that allows users to jump between tests and corresponding test objects. This feature will help users write unit tests more conveniently.
Code manipulation-easier to generate constructors and coverage/implementation methods
As we mentioned in the previous blog , we will always strive to make common code operations easier to use. In the latest version, users can now use the "light bulb icon" next to the Java class to easily generate constructors or override/implement methods! Here is a quick demo:
"Dealing" with garbled problems
It is very common for users to encounter certain coding problems when dealing with various languages. We did some analysis after hearing such feedback, so in this blog we want to share our findings and suggestions.
background
The computer can only understand binary data such as 0 and 1, and it uses a character set to encode/decode the data into real-world characters. When two processes are performing I/O interaction, they must use compatible character sets for encoding and decoding, otherwise garbled characters may appear. MacOS and Linux use UTF-8 everywhere, so encoding is not a problem for them. However, for Windows, the default character set is not UTF-8 and is platform-dependent, which will cause inconsistent encodings between different tools.
common problem
The following are typical coding problems when running a Java program on a Windows terminal.
- The file or directory name contains Unicode characters, and the Java launcher cannot find the corresponding class path or main class.
中文目录
├── Hello.class
└── Hello.java
C:\Test>java -cp 中文目录 Hello
Error: Could not find or load main class Hello
- String text with Unicode characters will 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 appear when the Java program interacts with the terminal 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 the 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 findings and suggestions for dealing with such problems
Previously, in order to alleviate the encoding problem, we added some solutions on the Java Debugger side to force the use of UTF-8 in our tool chain. For example, add a launcher.bat to force the code page of the terminal to 65001, and set the default "file.encoding" property to "UTF-8". But the fact is, they do not systematically address coding issues, and also introduces some additional side effects (see # 756 , in the Microsoft / VSCode the Java-Debug-# 622 , in the Microsoft / vscode- java-debug#646 ).
After investigating the issue more, we found that the solution we added seemed unnecessary. Users only need to set the windows system locale to the language they want, and then the JVM and terminal will automatically change to an encoding compatible with your system locale. The official Java documentation ( https://www.java.com/en/download/help/locale.html ) also recommends this.
The following screenshot shows how to change the system locale in Windows. For example, if you want to use the terminal to enter Chinese characters in a Java program, you can set the Windows system locale to Chinese. The default Java character set will be "GBK" and the cmd code page will be "936", which will support Chinese characters well.
detailed document on how to deal with encoding issues.
Project management-say goodbye to metadata files such as ".project"
If you are using the Java extension package for Java development, we have good news for you-when you import a new Java project, Visual Studio Code no longer generates those hidden ".project" in the project path Metadata file! problem that has existed for more than three years, and we fixed it in the November version. If you are interested in knowing how we solved it, please visit this blog .
Year-end conclusion
We are approaching the end of 2021. In the past 12 months, we have been working hard to provide a better Java development experience on Visual Studio Code. By 2022, there will be more exciting things about Java support on Visual Studio Code, so stay tuned for future updates. As always, we thank users and the community for their support.
Feedback and suggestions
Please actively use our products! Your feedback and suggestions are very important to us and will help us do better. There are several ways to leave us feedback:
- Fill in the Chinese questionnaire
- Leave a comment under this article
- On our repo GitHub create Issue
Welcome to follow the Microsoft China MSDN subscription account for more latest releases!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。