简体   繁体   English

如何使用 IntelliJ IDEA 社区版调试 spring-boot 应用程序?

[英]How to debug spring-boot application with IntelliJ IDEA community Edition?

I'm having difficulties in debugging a Java spring-boot application on IntelliJ IDEA community Edition.我在调试 IntelliJ IDEA 社区版上的 Java spring-boot 应用程序时遇到了困难。 The main problem is, that the IDE won't stop on a breakpoint, even the program surely executes through it.主要问题是,IDE 不会在断点处停止,即使程序肯定会通过它执行。 How can I make the the IntelliJ IDEA to stop on the breakpoint?如何使 IntelliJ IDEA 在断点处停止?

As additional information, here is my run configurations:作为附加信息,这是我的运行配置:

Maven configuration with a command as: spring-boot:run. Maven 配置命令为:spring-boot:run。 Before launch I build the project.在启动之前,我构建了项目。

tldr: You can try tweaking the command line like this: tldr:您可以尝试像这样调整命令行:

spring-boot:run -Dspring-boot.run.fork=false

Explanation:说明:

When running the application in debug mode, the IntelliJ debugger attaches to the Java process that it starts itself (by appending the appropriate parameters, -agentlib:jdwp etc, to the Java command line).在调试模式下运行应用程序时,IntelliJ 调试器附加到它自己启动的 Java 进程(通过将适当的参数-agentlib:jdwp等附加到 Java 命令行)。

Quite often, these Java processes might then fork a new instance, which is not getting the same parameters, and because it is in a separate process, is not connected to the debugger.很多时候,这些 Java 进程可能会派生一个新实例,该实例没有获得相同的参数,并且因为它在一个单独的进程中,没有连接到调试器。 This can be confusing.这可能会令人困惑。

The spring-boot:run Maven goal, in addition to forking a new JVM, creates even more confusion, because it sometimes does fork and sometimes doesn't, depending on the options it gets, among other things. spring-boot:run Maven 目标除了 fork 一个新的 JVM 之外,还会造成更多的混乱,因为它有时会 fork 有时不会,这取决于它获得的选项等。 Some of this can be found in the documentation , but it's not always obvious.其中一些可以在文档中找到,但并不总是很明显。

You should first check whether the Java process actually is being debugged at all.您应该首先检查 Java 进程是否实际上正在被调试。 When you start the application from IntelliJ, you will see messages scrolling by in the Run / Debug tab.当您从 IntelliJ 启动应用程序时,您将在“运行/调试”选项卡中看到滚动的消息。 At the top, there's the command line that is being executed.在顶部,有正在执行的命令行。 It should contain the debugger parameters ( -agentlib:jdwp etc) and it should be followed by a message saying "Connected to the target VM", which is the debugger confirming that it has contact.它应该包含调试器参数( -agentlib:jdwp等),后面应该有一条消息说“连接到目标 VM”,这是调试器确认它有联系。

Next, if you are unsure if the JVM has been forked, you can check the process list in your OS, for example under MacOS and *nix you can use ps aux | grep java接下来,如果您不确定JVM是否已经分叉,您可以检查您操作系统中的进程列表,例如在MacOS和*nix下您可以使用ps aux | grep java ps aux | grep java . ps aux | grep java The Java processes typically have a giant parameter list, most of which is the class path. Java 进程通常有一个巨大的参数列表,其中大部分是类路径。 The actual application being run is at the very end of the command line.正在运行的实际应用程序位于命令行的最后。 If the JVM was forked, you have the process running the Maven goal, and another one running the Spring application.如果 JVM 是分叉的,那么您有一个运行 Maven 目标的进程,另一个运行 Spring 应用程序的进程。 Then your debugger will be connected to the process you are not interested in, and your breakpoints won't work.然后您的调试器将连接到您感兴趣的进程,并且您的断点将不起作用。

To stop spring-boot:run from forking, you can use the fork parameter above.要停止spring-boot:run分叉,您可以使用上面的fork参数。

The only approach that worked for me, is running or debugging application directly from Intellij Idea.唯一对我有用的方法是直接从 Intellij Idea 运行或调试应用程序。 Just open class which contains只需打开包含

 public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

And click right mouse button->Debug my application然后单击鼠标右键->调试我的应用程序

For me these steps work:对我来说,这些步骤有效:

  1. Select menu Run -> Edit Configurations...选择菜单运行 -> 编辑配置...
  2. Create new Remote Configuration.创建新的远程配置。 By default you don't need to change settings:默认情况下,您不需要更改设置:
    -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 . -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 But if you want for example to suspend JVM before you connects, you can change suspend=y .但是,例如,如果您想在连接之前暂停 JVM,则可以更改suspend=y Or you can chage port etc.或者您可以更改端口等。
  3. Copy command line depending your JVM version and save configuration.根据您的 JVM 版本复制命令行并保存配置。
  4. In Terminal window run your app with (in Maven usage case and JVM 1.5 and higher) mvn clean spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"在终端窗口中运行您的应用程序(在 Maven 用例和 JVM 1.5 及更高版本中) mvn clean spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend= n,地址=5005"
  5. Connect to your app by running your Remote Configuration created prviously on step 2. Now you can debug your app.通过运行先前在第 2 步中创建的远程配置连接到您的应用程序。现在您可以调试您的应用程序。

I found that including Spring Dev Tools in my build caused IntelliJ debugging to break (per your description above).我发现在我的构建中包含 Spring Dev Tools 会导致 IntelliJ 调试中断(根据您上面的描述)。 If you don't use this feature, then simply remove it from your build.如果您不使用此功能,则只需将其从您的构建中删除即可。

If using Maven, the lines below should be removed from you pom.xml.如果使用 Maven,则应从 pom.xml 中删除以下行。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

The only way I got it working was by creating a separate, remote debug configuration.我让它工作的唯一方法是创建一个单独的远程调试配置。

So go to edit configurations-> Remote -> +.所以去编辑配置->远程->+。 Then start your application normally through intelliJ.然后通过intelliJ正常启动您的应用程序。 Then switch to the newly created remote configuration.然后切换到新创建的远程配置。 Instead of running it, press debug.而不是运行它,按调试。 Now debugger should be ready, and you can set breakpoints and the debugger will stop to them.现在调试器应该准备好了,你可以设置断点,调试器会停止。

EDIT: For me the debug port was already enabled, but I see that this is not the case for everyone.编辑:对我来说,调试端口已经启用,但我发现并不是每个人都这样。 If you get an error like如果您收到类似的错误

'Unable to open debugger port (localhost:5005): java.net.ConnectException "Connection refused: connect"

Then you need to enable port on your app's pom.xml.然后你需要在你的应用程序的 pom.xml 上启用端口。 Copied from @Gianluca Musa answer:复制自@Gianluca Musa 回答:

<plugin>
<groupId>org.springframework.boot</groupId>
<configuration>
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
</jvmArguments>
</configuration>
</plugin>

Kudos for @Gianluca Musa for pointing this out in his answer感谢@Gianluca Musa 在他的回答中指出这一点

piphonom's anwser is good , but you need do a little more,which is add the jvmArguments to the maven plugin like this piphonom 的 anwser 很好,但是你需要做更多的事情,就是像这样将 jvmArguments 添加到 maven 插件中

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>
            -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
        </jvmArguments>
    </configuration>
</plugin>

for more information about remote debuge for spring boot project, read this有关 Spring Boot 项目远程调试的更多信息,请阅读本文

  1. enable the debug port on your app's pom.XML like:在您的应用程序的 pom.XML 上启用调试端口,例如:

    \n\n

    <plugin> <插件>\n<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>\n<configuration> <配置>\n<jvmArguments> <jvmArguments>\n-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005\n</jvmArguments> </jvmArguments>\n</configuration> </配置>\n</plugin> </插件>

  1. follow the Ville Miekk-oja sugestion遵循 Ville Miekk-oja sugestion

    So go to edit configurations-> Remote -> +.所以去编辑配置->远程->+。 Then start your application normally through intelliJ.然后通过intelliJ正常启动您的应用程序。 Then switch to the newly created remote configuration.然后切换到新创建的远程配置。 Instead of running it, press debug.而不是运行它,按调试。 Now debugger should be ready, and you can set breakpoints and the debugger will stop to them.现在调试器应该准备好了,你可以设置断点,调试器会停止。

Unfortunately, all the prevoius answers are incomplete.不幸的是,所有以前的答案都不完整。 I've spent much time to find the correct way of remote debuging in IntelliJ and here is the full explanation.我花了很多时间在 IntelliJ 中找到正确的远程调试方法,这里是完整的解释。

We assume that the project code is in your local machine (Windows OS) and you have a deployment of your project on an Ubuntu VM in your server (or your VMWare workstation).我们假设项目代码在您的本地机器(Windows 操作系统)中,并且您在服务器(或您的 VMWare 工作站)的 Ubuntu VM 上部署了您的项目。 and these two machines are in the same network (they can ping eachother)并且这两台机器在同一个网络中(它们可以互相ping通)

First of all, add the a new Run/Debug configuration using the menu Run>Edit Configuration and then hit the + button at the top left corner and choose the "Remote" option.首先,使用菜单 Run>Edit Configuration 添加新的 Run/Debug 配置,然后点击左上角的 + 按钮并选择“Remote”选项。 Keep the configuration parameters as is and just define a name for your new config.保持配置参数不变,只需为新配置定义一个名称。

Secondly, open putty and connect to your remote server over SSH.其次,打开 putty 并通过 SSH 连接到您的远程服务器。 run below command to add remote debugging feature to your project in the putty terminal:运行以下命令,在 putty 终端中为您的项目添加远程调试功能:

export JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

Now change to your project's root directory on the remote server (in the same putty session) and run it using the command you usually use to do it (mine is as following for example):现在切换到远程服务器上项目的根目录(在同一个 putty 会话中)并使用您通常用来执行它的命令运行它(例如我的如下):

mvn -U clean spring-boot:run

Here comes the most important part that everybody neglected here :)这里是每个人都忽略的最重要的部分:)

Right click on the top of the putty session window and select "Change Settings.." option.右键单击 Putty 会话窗口的顶部,然后选择“更改设置...”选项。 Go to the path Connection>SSH>Tunnels in the left side options tree.转到左侧选项树中的路径 Connection>SSH>Tunnels。 Now add two port forwarding records such as the following picture (one Local, which forwards the localhost 5005 port to your remote server IP with the same port number, and one Remote who forwards the remote 5005 port to the 5005 port on localhost machine)现在添加两条端口转发记录如下图(一个Local,将localhost 5005端口转发到你的同端口号的远程服务器IP,一个Remote,将远程5005端口转发到localhost机器上的5005端口)

在此处输入图片说明

Finally go back to IntelliJ and from the run menu choose your previously added configuration and then hit the Debug button.最后回到 IntelliJ 并从运行菜单中选择您之前添加的配置,然后点击 Debug 按钮。 Your local IntelliJ IDEA should be connected to the remote deployment of your project now, ready to debug!!你本地的 IntelliJ IDEA 现在应该已经连接到你项目的远程部署了,准备调试!!

在此处输入图片说明

on inteliJ goto run-> edit configuration -> press on the '+' -> choose 'Application'在 inteliJ 上转到运行-> 编辑配置-> 按“+”-> 选择“应用程序”

fill the fields: main class,working directory, classpath of module填写字段:主类、工作目录、模块的类路径

Spring boot maven plugin (> 2.2.0) forks application process. Spring boot maven plugin (> 2.2.0) fork应用进程。 So the good old "spring-boot:run" started in debug mode doesn't stop on breakpoints.因此,在调试模式下启动的旧“spring-boot:run”不会在断点处停止。

You have 2 options:您有 2 个选择:

1. Directly run main class See application configuration 1.直接运行主类见应用配置

2. Remote debugging 2.远程调试

2.1. 2.1. Configure spring-boot-maven-plugin to enable remote debug配置 spring-boot-maven-plugin 启用远程调试

<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=5005
                </jvmArguments>
            </configuration>
        </plugin>
     </plugins>
</build>

2.2. 2.2. Run server See maven configuration运行服务器查看maven配置

2.3. 2.3. Run debug-server See remote JVM debug configuration运行 debug-server查看远程 JVM 调试配置

You can use this workaround in IntelliJ Community edition to debug java application with standard module (through mvn spring-boot:run) without needing to create a dedicated debug listener in intelliJ configuration (it will be created on the fly):您可以在 IntelliJ 社区版中使用此解决方法来使用标准模块(通过 mvn spring-boot:run)调试 java 应用程序,而无需在 IntelliJ 配置中创建专用调试侦听器(它将即时创建):

In the maven menu (by default on the right of the screen), click on the "execute maven goal" button:在maven菜单(默认在屏幕右侧)中,点击“执行maven目标”按钮:

单击箭头指向的按钮

Then enter this goal: mvn spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5000" (you may want to change the port for the coordination between intelliJ's debugger and your application)然后输入这个目标: mvn spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5000" (您可能需要更改端口intelliJ 的调试器和您的应用程序之间的协调)

This will start your Spring boot application but it will suspend.这将启动您的 Spring 引导应用程序,但它将暂停。 In the run menu below, you'll see a button Attach debugger .在下面的run菜单中,您会看到一个按钮Attach debugger

单击图像底部的箭头指向的按钮

Click on it and voilà: your spring-boot application will run and the breakpoints will hit.单击它并瞧瞧:您的 spring-boot 应用程序将运行并且断点将命中。

NB:the solution provided by @stinger would only work in IntelliJ Idea Ultimate Edition, it will not work in IntelliJ Community Edition, which is the one asked in the question, but I don't have enough reputation yet to add a comment on his answer.注意:@stinger 提供的解决方案仅适用于 IntelliJ Idea Ultimate Edition,它不适用于 IntelliJ Community Edition,这是问题中提出的问题,但我还没有足够的声誉来对他的评论添加评论回答。 His solution is yet preferred if you use Ultimate Edition如果您使用终极版,他的解决方案仍然是首选

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM