代码运行器扩展只能让您“运行”Java文件。
如果要真正调试Java文件,请进行一次快速的设置:
- 在VS Code中安装Java Debugger Extension并重新加载。
- 在VS Code中打开一个空文件夹/项目。
- 创建您的Java文件。
- 在同一文件夹中创建一个名为
.vscode
的文件夹。
- 在
.vscode
文件夹内创建2个文件:tasks.json
和launch.json
- 将下面的配置复制粘贴到
tasks.json
中:
{
"version": "2.0.0",
"type": "shell",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"isBackground": true,
"tasks": [
{
"taskName": "build",
"args": ["-g", "${file}"],
"command": "javac"
}
]
}
- 将以下配置复制并粘贴到
launch.json
文件中:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Java",
"type": "java",
"request": "launch",
"externalConsole": true,
"stopOnEntry": true,
"preLaunchTask": "build",
"jdkPath": "${env:JAVA_HOME}/bin",
"cwd": "${workspaceRoot}",
"startupClass": "${workspaceRoot}${file}",
"sourcePath": ["${workspaceRoot}"],
"classpath": ["${workspaceRoot}"],
"options": [],
"args": []
}
],
"compounds": []
}
您已经可以调试Java文件,只需要打开任何Java文件并按下F5(Debug->Start Debugging)即可。
提示:*为了在VS code的侧边资源管理器中隐藏.class文件,打开VS code的settings
并粘贴以下配置:
"files.exclude": {
"*.class": true
}
![enter image description here](https://istack.dev59.com/GR07m.gif)