代码运行程序扩展将只允许您“运行”java文件。
要真正调试“Java”文件,请遵循快速的一次性设置:
- 在 VS Code 中安装 Java 调试器扩展并重新加载。
- 在 VS 代码中打开一个空文件夹/项目。
- 创建您的 java 文件。
- 在同一文件夹中创建一个文件夹。
.vscode
- 在文件夹内创建2个文件:和
.vscode
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"
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Java",
"type": "java",
"request": "launch",
"externalConsole": true, //user input dosen't work if set it to false :(
"stopOnEntry": true,
"preLaunchTask": "build", // Runs the task created above before running this configuration
"jdkPath": "${env:JAVA_HOME}/bin", // You need to set JAVA_HOME enviroment variable
"cwd": "${workspaceRoot}",
"startupClass": "${workspaceRoot}${file}",
"sourcePath": ["${workspaceRoot}"], // Indicates where your source (.java) files are
"classpath": ["${workspaceRoot}"], // Indicates the location of your .class files
"options": [], // Additional options to pass to the java executable
"args": [] // Command line arguments to pass to the startup class
}
],
"compounds": []
}
您都已准备好调试 Java 文件,打开任何 java 文件,然后按 F5(调试>启动调试)。
提示: *要在VS代码的侧面资源管理器中隐藏.class文件,请打开VS代码的设置
并粘贴以下配置:
"files.exclude": {
"*.class": true
}