Java 如何使用 Eclipse 调试 Spring Boot 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24113939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:13:43  来源:igfitidea点击:

How to debug Spring Boot application with Eclipse?

javaspringeclipsedebuggingspring-boot

提问by JeffLL

My Spring Bootwebapp is running just fine, and I'd like to debug it through Eclipse.

我的Spring Bootwebapp 运行得很好,我想通过 Eclipse 调试它。

So when launching my Remote Java Application debugger, which port should I listen to? And is there a setting on my webapp I have to set to enable debugging?

那么在启动我的远程 Java 应用程序调试器时,我应该监听哪个端口?我的 webapp 上是否有设置我必须设置以启用调试?

采纳答案by Dave Syer

Why don't you just right click on the main()method and choose "Debug As... Java Application"?

为什么不直接右键单击main()方法并选择"Debug As... Java Application"

回答by Sangram Jadhav

Please see http://java.dzone.com/articles/how-debug-remote-java-applicatto enable the remote debugging. If you are using tomcat to run your application, start tomcat with remote debug parameters or you can start tomcat with JPDA support by using following command.

请参阅http://java.dzone.com/articles/how-debug-remote-java-applict以启用远程调试。如果您使用 tomcat 运行您的应用程序,请使用远程调试参数启动 tomcat,或者您可以使用以下命令启动支持 JPDA 的 tomcat。

Windows

视窗

<tomcat bin dir>/startup.bat jpda

*nix

*尼克斯

<tomcat bin dir>/startup.sh jpda

this will enable remote debugging on port 8000

这将在端口 8000 上启用远程调试

回答by masster

There's section 19.2in Spring Boot Referencethat tells you about starting your application with remote debugging support enabled.

部分19.2春季启动参考,告诉你有关远程调试支持启用启动应用程序。

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
   -jar target/myproject-0.0.1-SNAPSHOT.jar

After you start your application just add that Remote Java Application configurationin Run/Debug configurations, select the port/address you defined when starting your app, and then you are free to debug.

启动应用程序后,只需在运行/调试配置中添加远程 Java 应用程序配置,选择启动应用程序时定义的端口/地址,然后就可以自由调试了。

回答by Dawngerpony

I didn't need to set up remote debugging in order to get this working, I used Maven.

我不需要设置远程调试来使其正常工作,我使用了 Maven。

  1. Ensure you have the Maven plugin installed into Eclipse.
  2. Click Run > Run Configurations > Maven Build > new launch configuration:
    • Base directory: browse to the root of your project
    • Goals: spring-boot::run.
  3. Click Applythen click Run.
  1. 确保您已将 Maven 插件安装到 Eclipse 中。
  2. 单击运行 > 运行配置 > Maven 构建 > 新启动配置
    • 基目录:浏览到项目的根目录
    • 目标:spring-boot::run
  3. 单击应用,然后单击运行

NB. If your IDE has problems finding your project's source code when doing line-by-line debugging, take a look at this SO answerto find out how to manually attach your source code to the debug application.

注意。如果您的 IDE 在进行逐行调试时在查找项目源代码时遇到问题,请查看此 SO 答案以了解如何手动将源代码附加到调试应用程序。

Hope this helps someone!

希望这可以帮助某人!

回答by krmanish007

The best solution in my opinion is add a plugin in the pom.xml, and you don't need to do anything else all the time:

我认为最好的解决方案是在 pom.xml 中添加一个插件,并且您不需要一直做其他任何事情:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>
                        -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9898
                    </jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

回答by Duran Wesley Harris

Easier solution:

更简单的解决方案:

Instead of typing mvn spring-boot:run, simply type mvnDebug spring-boot:run

无需键入mvn spring-boot:run,只需键入mvnDebug spring-boot:run

You will still need to attach the debugger in Eclipse by making a new Debug Configuration for a "Remote Java Application" on the relevant port.

您仍然需要通过在相关端口上为“远程 Java 应用程序”创建新的调试配置来在 Eclipse 中附加调试器。

回答by Arpit Aggarwal

Run below command where pom.xmlis placed:

pom.xml放置的位置运行以下命令:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

And start your remote java applicationwith debugging option on port 5005

并在端口上使用调试选项启动远程 Java 应用程序5005

回答by Abdull

How to debug a remote staging or production Spring Boot application

如何调试远程登台或生产 Spring Boot 应用程序

Server-side

服务器端

Let's assume you have successfully followed Spring Boot's guide on setting up your Spring Boot application as a service. Your application artifact resides in /srv/my-app/my-app.war, accompanied by a configuration file /srv/my-app/my-app.conf:

假设您已成功遵循Spring Boot 的指南,将您的 Spring Boot 应用程序设置为服务。您的应用程序工件驻留在 中/srv/my-app/my-app.war,并附有一个配置文件/srv/my-app/my-app.conf

# This is file my-app.conf
# What can you do in this .conf file? The my-app.war is prepended with a SysV init.d script
# (yes, take a look into the war file with a text editor). As my-app.war is symlinked in the init.d directory, that init.d script
# gets executed. One of its step is actually `source`ing this .conf file. Therefore we can do anything in this .conf file that
# we can also do in a regular shell script.

JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,address=localhost:8002,server=y,suspend=n"
export SPRING_PROFILES_ACTIVE=staging

When you restart your Spring Boot application with sudo service my-app restart, then in its log file located at /var/log/my-app.logshould be a line saying Listening for transport dt_socket at address: 8002.

当您重新启动您的春季启动应用程序sudo service my-app restart,然后在位于其日志文件/var/log/my-app.log应该是一条线说Listening for transport dt_socket at address: 8002

Client-side (developer machine)

客户端(开发者机器)

Open an SSH port-forwarding tunnel to the server: ssh -L 8002:localhost:8002 [email protected]. Keep this SSH session running.

打开到服务器的 SSH 端口转发隧道:ssh -L 8002:localhost:8002 [email protected]。保持此 SSH 会话运行。

In Eclipse, from the toolbar, select Run-> Debug Configurations-> select Remote Java Application-> click the Newbutton -> select as Connection Type Standard (Socket Attach), as Host localhost, and as Port 8002(or whatever you have configured in the steps before). Click Applyand then Debug.

在 Eclipse 中,从工具栏中选择Run-> Debug Configurations-> 选择Remote Java Application-> 单击New按钮 -> 选择 Connection Type Standard (Socket Attach)、 Host localhost和 Port 8002(或任何你拥有的在之前的步骤中配置)。单击应用,然后单击调试

The Eclipse debugger should now connect to the remote server. Switching to the Debugperspective should show the connected JVM and its threads. Breakpoints should fire as soon as they are remotely triggered.

Eclipse 调试器现在应该连接到远程服务器。切换到Debug透视图应该会显示连接的 JVM 及其线程。断点应在远程触发后立即触发。

回答by adia

With eclipse or any other IDE, just right click on your main spring boot application and click "debug as java application"

使用 Eclipse 或任何其他 IDE,只需右键单击您的主要 Spring Boot 应用程序,然后单击“调试为 Java 应用程序”

回答by bharatpatel

This question is already answered, but i also got same issue to debug Springboot + gradle + jHipster,

这个问题已经回答了,但我在调试 Springboot + gradle + jHipster 时也遇到了同样的问题,

Mostly Spring boot application can debug by right click and debug, but when you use gradle, having some additional environment parameter setup then it is not possible to debug directly.

大多数 Spring boot 应用程序可以通过右键单击和调试来调试,但是当您使用 gradle 时,如果设置了一些额外的环境参数,则无法直接调试。

To resolve this, Eclipse provided one additional features as Remote Java Application

为了解决这个问题,Eclipse 提供了一项附加功能作为远程 Java 应用程序

by using this features you can debug your application.

通过使用此功能,您可以调试您的应用程序。

Follow below step:

请按照以下步骤操作:

run your gradle application with ./gradlew bootRun --debug-jvmcommand

使用./gradlew bootRun --debug-jvm命令运行您的 gradle 应用程序

Now go to eclipse --> right click project and Debug configuration --> Remote Java Application.

现在转到 eclipse --> 右键单击​​项目和调试配置 --> 远程 Java 应用程序。

add you host and port as localhost and port as 5005 (default for gradle debug, you can change it)

将主机和端口添加为 localhost,将端口添加为 5005(gradle 调试的默认值,您可以更改它)

Referfor more detail and step.

有关更多详细信息和步骤,请参阅