首页 > 解决方案 > 如何使用 Visual Studio Code 调试 java dropwizard 应用程序

问题描述

设置 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 文件):

  1. 更改 launch.json 以便附加命令使用端口 8000(mvnDebug 的默认值)
  2. 在终端中,使用以下命令启动应用程序:mvnDebug exec:java -Dexec.mainClass="com.example.helloworld.HelloWorldApplication" -Dexec.args="server example.yml"
  3. 运行 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

dropwizard-示例调试工作

并且还设置断点!

标签: javadebuggingvisual-studio-codedropwizard

解决方案


添加另一个配置以启动配置(.vscode 中的launch.json),如下所示 -

{
            "type": "java",
            "name": "Debug (Launch) with Arguments Prompt",
            "request": "launch",
            "mainClass": "{mainClass}",
            "args": "server config.yml"
}

推荐阅读