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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44096708/
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-12 01:20:15  来源:igfitidea点击:

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

javaintellij-ideaspring-boot

提问by Ville Miekk-oja

I'm having difficulties in debugging a Java spring-boot application on IntelliJ IDEA community Edition. The main problem is, that the IDE won't stop on a breakpoint, even the program surely executes through it. How can I make the the IntelliJ IDEA to stop on the breakpoint?

我在 IntelliJ IDEA 社区版上调试 Java spring-boot 应用程序时遇到困难。主要问题是,IDE 不会在断点处停止,即使程序肯定会通过它执行。如何让 IntelliJ IDEA 在断点处停止?

As additional information, here is my run configurations:

作为附加信息,这是我的运行配置:

Maven configuration with a command as: spring-boot:run. Before launch I build the project.

Maven 配置命令为:spring-boot:run。在启动之前,我构建了项目。

采纳答案by Ville Miekk-oja

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. 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.

所以去编辑配置->远程->+。然后通过intelliJ正常启动您的应用程序。然后切换到新创建的远程配置。而不是运行它,按调试。现在调试器应该准备好了,你可以设置断点,调试器会停止。

回答by piphonom

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

回答by Abdullah Gürsu

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:jdwpetc, to the Java command line).

在调试模式下运行应用程序时,IntelliJ 调试器附加到它自己启动的 Java 进程(通过将适当的参数-agentlib:jdwp等附加到 Java 命令行)。

Quite often, these Java processes might then fork a new instance, which is notgetting the same parameters, and because it is in a separate process, is not connected to the debugger. This can be confusing.

很多时候,这些 Java 进程可能会派生一个新实例,该实例没有获得相同的参数,并且因为它在一个单独的进程中,没有连接到调试器。这可能会令人困惑。

The spring-boot:runMaven 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. Some of this can be found in the documentation, but it's not always obvious.

spring-boot:runMaven的目标,除了派生一个新的JVM,创造更加混乱,因为它有时做叉,有时并不取决于它获取,除其他事项外的选项。其中一些可以在文档中找到,但并不总是很明显。

You should first check whether the Java process actually is being debugged at all. When you start the application from IntelliJ, you will see messages scrolling by in the Run / Debug tab. At the top, there's the command line that is being executed. It should contain the debugger parameters (-agentlib:jdwpetc) and it should be followed by a message saying "Connected to the target VM", which is the debugger confirming that it has contact.

您应该首先检查 Java 进程是否实际上正在被调试。当您从 IntelliJ 启动应用程序时,您将在“运行/调试”选项卡中看到滚动的消息。在顶部,有正在执行的命令行。它应该包含调试器参数(-agentlib:jdwp等),并且应该跟随一条消息“连接到目标虚拟机”,这是调试器确认它有联系。

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. The Java processes typically have a giant parameter list, most of which is the class path. 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. Then your debugger will be connected to the process you are notinterested in, and your breakpoints won't work.

接下来,如果您不确定 JVM 是否已分叉,您可以检查您操作系统中的进程列表,例如在 MacOS 和 *nix 下您可以使用ps aux | grep java. Java 进程通常有一个巨大的参数列表,其中大部分是类路径。正在运行的实际应用程序位于命令行的最后。如果 JVM 是分叉的,那么您有一个运行 Maven 目标的进程,另一个运行 Spring 应用程序的进程。然后您的调试器将连接到您感兴趣的进程,并且您的断点将不起作用。

To stop spring-boot:runfrom forking, you can use the forkparameter above.

要停止spring-boot:run分叉,您可以使用fork上面的参数。

回答by stinger

The only approach that worked for me, is running or debugging application directly from Intellij Idea. Just open class which contains

唯一对我有用的方法是直接从 Intellij Idea 运行或调试应用程序。只需打开包含

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

And click right mouse button->Debug my application

然后单击鼠标右键->调试我的应用程序

回答by kite

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 项目远程调试的更多信息,请阅读本文

回答by Burton

I found that including Spring Dev Tools in my build caused IntelliJ debugging to break (per your description above). If you don't use this feature, then simply remove it from your build.

我发现在我的构建中包含 Spring Dev Tools 会导致 IntelliJ 调试中断(根据您上面的描述)。如果您不使用此功能,则只需将其从您的构建中删除即可。

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>

回答by Ziv.Ti

on inteliJ goto run-> edit configuration -> press on the '+' -> choose 'Application'

在 inteliJ 上转到运行-> 编辑配置-> 按“+”-> 选择“应用程序”

fill the fields: main class,working directory, classpath of module

填写字段:主类、工作目录、模块的类路径

回答by Gianluca Musa

  1. enable the debug port on your app's pom.XML like:

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

  1. 在您的应用程序的 pom.XML 上启用调试端口,例如:

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

  1. follow the Ville Miekk-oja sugestion

    So go to edit configurations-> Remote -> +. Then start your application normally through 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.

  1. 遵循 Ville Miekk-oja sugestion

    所以去编辑配置->远程->+。然后通过intelliJ正常启动您的应用程序。然后切换到新创建的远程配置。而不是运行它,按调试。现在调试器应该准备好了,你可以设置断点,调试器会停止。

回答by Majid

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). and these two machines are in the same network (they can ping eachother)

我们假设项目代码在您的本地机器(Windows 操作系统)中,并且您在服务器(或您的 VMWare 工作站)的 Ubuntu VM 上部署了您的项目。并且这两台机器在同一个网络中(它们可以互相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. Keep the configuration parameters as is and just define a name for your new config.

首先,使用菜单 Run>Edit Configuration 添加新的 Run/Debug 配置,然后点击左上角的 + 按钮并选择“Remote”选项。保持配置参数不变,只需为新配置定义一个名称。

Secondly, open putty and connect to your remote server over SSH. run below command to add remote debugging feature to your project in the putty terminal:

其次,打开 putty 并通过 SSH 连接到您的远程服务器。运行以下命令,在 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. Go to the path Connection>SSH>Tunnels in the left side options tree. 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)

右键单击 Putty 会话窗口的顶部,然后选择“更改设置...”选项。转到左侧选项树中的路径 Connection>SSH>Tunnels。现在添加两条端口转发记录如下图(一个Local,将localhost 5005端口转发到你的同端口号的远程服务器IP,一个Remote,将远程5005端口转发到localhost机器上的5005端口)

enter image description here

在此处输入图片说明

Finally go back to IntelliJ and from the run menu choose your previously added configuration and then hit the Debug button. Your local IntelliJ IDEA should be connected to the remote deployment of your project now, ready to debug!!

最后回到 IntelliJ 并从运行菜单中选择您之前添加的配置,然后点击 Debug 按钮。你本地的 IntelliJ IDEA 现在应该已经连接到你项目的远程部署了,准备调试!!

enter image description here

在此处输入图片说明