85
votes

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.

10
Did you run it in debug mode?ian1095
Yes, I did.....Ville Miekk-oja
Here is a maven spring boot plugin docs.spring.io/spring-boot/docs/current/maven-plugin/examples/… Have you tried it?Andrew Tobilko
I have the spring-boot-maven-plugin. Though I don't have any configurations for it defined in pom.xml. Might try that one, if I understand it correctly mm...Ville Miekk-oja
@VilleMiekk-oja, if all is configured properly, open the "Maven Project" tab on the right side, and choose the bootRun option there with the right-clickAndrew Tobilko

10 Answers

120
votes

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.

75
votes

The only approach that worked for me, is running or debugging application directly from 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

56
votes

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 -Dspring-boot.run.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.
14
votes

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

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.

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. Copied from @Gianluca Musa answer:

<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

5
votes

piphonom's anwser is good , but you need do a little more,which is add the jvmArguments to the maven plugin like this

<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

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

3
votes

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

1
votes

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

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

0
votes

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