155

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.

asked May 21, 2017 at 12:13
10
  • 2
    Did you run it in debug mode? Commented May 21, 2017 at 12:14
  • 1
    Yes, I did..... Commented May 21, 2017 at 12:16
  • 1
    Here is a maven spring boot plugin docs.spring.io/spring-boot/docs/current/maven-plugin/examples/… Have you tried it? Commented May 21, 2017 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... Commented May 21, 2017 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 Commented May 21, 2017 at 12:25

13 Answers 13

213

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
16k14 gold badges73 silver badges109 bronze badges
answered Nov 1, 2017 at 22:01
Sign up to request clarification or add additional context in comments.

7 Comments

This resolved it for me. I tried the JAVA_OPTS and the Drun.jvmArguments without any success.
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!
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
Also un-check the "Delegate do maven" option
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
|
108

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

answered Nov 29, 2017 at 11:37

6 Comments

It would be interesting to have a look at IntelliJ's console and see what it is actually doing and why that would be different from the other ways to start the application with a debugger attached.
Idea adds its own debug configuration to JVM and starts debugging on arbitrary port rather then using debug properties pre-defined by you.
does not work in my case, intellij is still not stopping at breakpoints
This works for me. My project is springboot rest backend, jar package, with embed tomcat, Idea debug it perfectly!
That's not the problem described in question.
|
63

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
3004 silver badges15 bronze badges
answered Sep 4, 2017 at 12:49

3 Comments

In case someone is using copy-paste: -Drun:jvmArguments= should be -Drun.jvmArguments= (dot instead of colon)
I actually needed to add this to pom.xml, because spring-boot ignored the -Drun... argument: <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.3.RELEASE</version> <configuration> <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments> </configuration> </plugin>
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.
18

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

answered Jun 8, 2017 at 22:23

5 Comments

Excellent solution; works also on my setup: IntelliJ 2018.2, Maven 3.5.4 to debug a spring-boot 1.3.6 app (Java 8). Kudos.
Unable to open debugger port (localhost:5005): java.net.ConnectException "Connection refused: connect"
Still works fine for IntelliJ Ultimate version 2020.3
Works like a charm. Note that you also need to add <artifactId>spring-boot-maven-plugin</artifactId>, otherwise it will fail with "artifactId of a plugin must be defined".
Still works on IntelliJ Community 2023年1月2日, thaks
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>
answered Aug 20, 2018 at 21:32

1 Comment

This import fact is not reflected in spring-devtools documentation docs.spring.io/spring-boot/docs/current/reference/html/… . Thank you.
9

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. @stinger 's solution is yet preferred if you use Ultimate Edition

answered Aug 31, 2022 at 14:07

3 Comments

Thanks a lot, this is working for me (IntelliJ Community Edition). Finally I can debug and learn Java the way I want.
@MarcoSulla you don't, it's in a different tab
There is a Console tab, but it's empty, no output. The debugger works fine!
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

answered Dec 26, 2017 at 2:13

2 Comments

It was not helpful. Maybe the debug port is random and its not bound by maven plugin configuration
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
5

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

answered Feb 28, 2021 at 9:52

Comments

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.

answered Feb 7, 2019 at 9:13

Comments

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

answered Mar 9, 2019 at 15:25

1 Comment

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"
2

For me, I didn't actually have to run my project with mvn spring-boot:run. Instead of debugging with a maven configuration I went to Edit Configurations and added a spring boot configuration with the same parameters as my maven configuration. Out of the box in IntelliJ Idea 2023年3月2日 the debugging worked.

answered Mar 12, 2024 at 20:03

2 Comments

Could you show your new configurations?
@parsecer This was from simply "Add new configuration", I selected my correct JDK, for -cp i selected the project module I needed for the classpath, for the application I selected the Application class within my project (the one with the SpringBootApplication annotation). This was all I required
1

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)

answered Mar 1, 2023 at 12:57

Comments

0

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

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

answered Oct 23, 2018 at 5:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.