138

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.

Ville Miekk-oja
  • 18,749
  • 32
  • 70
  • 106
  • 2
    Did you run it in debug mode? – ian1095 May 21 '17 at 12:14
  • 1
    Yes, I did..... – Ville Miekk-oja May 21 '17 at 12:16
  • 1
    Here is a maven spring boot plugin http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html Have you tried it? – Andrew Tobilko May 21 '17 at 12:17
  • 1
    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 May 21 '17 at 12:20
  • 1
    @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-click – Andrew Tobilko May 21 '17 at 12:25
  • ERROR: transport error 202: bind failed: Address already in use. The last option (address), what address should I use? The same as the application uses, or the one specified in the link? (5005) – Ville Miekk-oja May 21 '17 at 12:32
  • @VilleMiekk-oja, it seems you could not close the previous start of the app clearly, or the port you are trying to use is busy. – Andrew Tobilko May 21 '17 at 12:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144772/discussion-between-ville-miekk-oja-and-andrew-tobilko). – Ville Miekk-oja May 21 '17 at 12:41
  • I hav the same occasionaly in Ultimate. I am not quite sure what it it, some perhaps process still running, but normally quiting and restarting IJ helps. – P.J.Meisch May 21 '17 at 13:40

12 Answers12

204

EDIT: it does not work anymore from Spring Boot 3+. See Ruik's comment


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.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
Abdullah Gürsu
  • 3,010
  • 2
  • 17
  • 12
  • 2
    This resolved it for me. I tried the `JAVA_OPTS` and the `Drun.jvmArguments` without any success. – Ashok Goli Sep 12 '19 at 16:48
  • 1
    This helped me to get over the problem of not being able to debug Spring Boot applications using IntelliJ IDEA 2019.3.4. Thanks a bunch! – Vesa Nieminen Apr 21 '20 at 09:34
  • Worked perfectly with the Ultimate Edition of Intellij IDEA too with a springboot maven project with spring-boot-maven-plugin plugin... ```jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005``` run debug command-line: ```spring-boot:run -Dspring-boot.run.fork=false -f pom.xml``` – Simone Celia Nov 05 '20 at 11:58
  • Also un-check the "Delegate do maven" option – lrkwz Nov 18 '20 at 15:22
  • 1
    For Spring Boot 3 this no longer works, because the fork option had beed deprecated with 2.7 and is now removed completely. See here: [Spring Boot 3 Migration Guide](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide) – Ruik Feb 15 '23 at 10:10
  • @Ruik So what do we do now? – TMOTTM May 04 '23 at 15:20
  • I found a workaround. Thanks for the Reminder to post this here: https://stackoverflow.com/questions/75483956/debug-spring-boot-3-in-intellij-community I also posted this answer on this question but it is very far down. – Ruik May 04 '23 at 15:23
95

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

stinger
  • 3,790
  • 1
  • 19
  • 30
61

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.
tonyfarney
  • 300
  • 3
  • 14
piphonom
  • 623
  • 6
  • 14
  • 7
    In case someone is using copy-paste: `-Drun:jvmArguments=` should be `-Drun.jvmArguments=` (dot instead of colon) – Roar Skullestad Feb 06 '18 at 12:42
  • 2
    I actually needed to add this to pom.xml, because spring-boot ignored the -Drun... argument: ` org.springframework.boot spring-boot-maven-plugin 2.0.3.RELEASE -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 ` – andraaspar Jul 27 '18 at 11:20
  • In Intellij you can also go to `Runner` tab in your Run Configuration and paste these arguments (`-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005`) into `VM Options` input. – luke May 30 '19 at 19:31
13

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>
Burton
  • 821
  • 8
  • 16
  • This import fact is not reflected in spring-devtools documentation https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html . Thank you. – Eugene Apr 01 '19 at 23:52
11

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

Ville Miekk-oja
  • 18,749
  • 32
  • 70
  • 106
6

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

Kai
  • 679
  • 9
  • 11
  • It was not helpful. Maybe the debug port is random and its not bound by maven plugin configuration – Abubacker Siddik Mar 26 '18 at 09:02
  • This worked great for me. The suspend=y caused the build to wait for the debugger to attach, which is great for debugging initialization-related issues as well. openjdk 15 2020-09-15 OpenJDK Runtime Environment (build 15+36-1562) OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing) Apache Maven 3.6.3 IntelliJ 2020.3.2 Spring Boot 2.4.2 – Will Iverson Feb 09 '21 at 21:43
4
  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.

Gianluca Musa
  • 755
  • 7
  • 22
4

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

Majid
  • 336
  • 2
  • 13
  • This works for me. Thanks! The only difference is that my project already has its JAVA_OPTS, so I end up adding this line to my shell script: `export JAVA_OPTS="${JAVA_OPTS} -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"` – Tao Zhang Mar 21 '19 at 19:55
4

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

3

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

1

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

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

Ziv.Ti
  • 609
  • 7
  • 10
0

For Spring Boot 3 the spring-boot.run.fork=false way no longer works. Unfortunatly this option was removed as you can read in the [Spring Boot 3.0 Migration Guide][1]:

The fork attribute of spring-boot:run and spring-boot:start that was deprecated in Spring Boot 2.7 has been removed.

Since starting the Spring Boot Main class didn't work for me and attaching a debugger was both complicated and only possible when the server was already running, I found another workaround. It my not be ideal in any cases but probably an easy way for many cases.

I do a @SpringBootTest with JUnit and then a test method that just sleeps for one our. That way the Spring Context comes up and stays for debugging. If I let Spring bind to a defined port I can even call REST endpoints.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class TemplateApplicationTests {

    @Test
    void contextLoads() throws InterruptedException {
        Thread.sleep(360000);
    }
}

(This is the same answer that I posted to my own question regarding the Spring Boot 3 problem: Debug Spring Boot 3 in IntelliJ Community)

Ruik
  • 1,222
  • 1
  • 20
  • 36