【发布时间】:2018-12-02 21:09:36
【问题描述】:
设置 Visual Studio Code 以使用 Java 扩展包调试 Java Dropwizard 应用程序需要哪些额外步骤?我正在按照官方 vscode-java-debug 页面上的说明进行操作:
https://code.visualstudio.com/blogs/2017/09/28/java-debug
针对位于此处的官方 dropwizard-example 应用程序:
https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example
当我在 vscode 中设置默认 lauch 配置文件然后尝试运行它时,调试控制台会打印以下消息:
usage: java -jar project.jar [-h] [-v] {server,check,render,db} ... positional arguments: {server,check,render,db} available commands named arguments: -h, --help show this help message and exit -v, --version show the application version and exit
dropwizard 应用程序的启动方式似乎存在根本不同,导致默认的 vscode 调试设置不起作用。我猜想需要一些自定义启动任务,但我找不到其他使用 vscode 来处理 dropwizard 应用程序的人。
dropwizard 应用程序本身使用其 wiki 中的说明成功运行 - 我无法使用默认的 vscode 调试说明进行调试。我已经使用 vscode 调试说明页面上提供的相同步骤成功调试了另一个 java 应用程序。这是第一次尝试时完美运行的项目(Sprint Boot):
https://github.com/spring-guides/gs-spring-boot
更新
(注意:我仍然有兴趣学习如何连接 vscode 以启动应用程序,这样我还可以执行调试应用程序初始化等操作)
我找到了一种启动应用程序的方法,您可以使用默认的 vscode 启动配置附加到正在运行的进程。这是我偶然发现的 wiki 页面,它展示了如何使用 mvnDebug:
https://github.com/Microsoft/vscode-java-debug/wiki/How-to-attach-debug-maven-console-applications
该 wiki 页面中的说明几乎就是我们所需要的。请改为执行以下操作(假设您已经按照上面 vscode wiki 中的说明获取 java 的默认 launch.json 文件):
- 更改 launch.json 以便 attach 命令使用端口 8000(mvnDebug 的默认端口)
- 在终端中,使用以下命令启动应用程序:
mvnDebug exec:java -Dexec.mainClass="com.example.helloworld.HelloWorldApplication" -Dexec.args="server example.yml" - 运行 vscode 附加命令
launch.json 应该是这样的:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch)-HelloWorldApplication<dropwizard-example>",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "com.example.helloworld.HelloWorldApplication",
"projectName": "dropwizard-example",
"args": ""
},
{
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 8000
}
]
}
那么你应该能够像以前一样点击应用程序,但现在可以设置断点:
http://localhost:8080/hello-world
还要设置断点!
【问题讨论】:
标签: java debugging visual-studio-code dropwizard