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

Viewed 131571

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?

As additional information, here is my run configurations:

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

11 Answers

tldr: You can try tweaking the command line like this:

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

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

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 not interested in, and your breakpoints won't work.

To stop spring-boot:run from forking, you can use the fork parameter above.

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.

If using Maven, the lines below should be removed from you pom.xml.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
  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. 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.

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.

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)

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.

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:

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):

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)

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

enter image description here

Spring boot maven plugin (> 2.2.0) forks application process. So the good old "spring-boot:run" started in debug mode doesn't stop on breakpoints.

You have 2 options:

1. Directly run main class See application configuration

2. Remote debugging

2.1. Configure spring-boot-maven-plugin to enable remote debug

<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. Run server See maven configuration

2.3. Run debug-server See remote JVM debug configuration

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

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

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) :

In the maven menu (by default on the right of the screen), click on the "execute maven goal" button :

click the button the arrow points to

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)

This will start your Spring boot application but it will suspend. In the run menu below, you'll see a button Attach debugger.

click the button the arrow points to, at the bottom of the image

Click on it and voilĂ : your spring-boot application will run and the breakpoints will hit.

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. His solution is yet preferred if you use Ultimate Edition

Related