117

My Spring Boot webapp is running just fine, and I'd like to debug it through Eclipse.

So when launching my Remote Java Application debugger, which port should I listen to? And is there a setting on my webapp I have to set to enable debugging?

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
JeffLL
  • 1,875
  • 3
  • 19
  • 30
  • In STS 3.9.4.RELEASE on Linux Mint 18 Sarah I have to go into the debug perspective to debug Spring Boot applications. – TimeTrap Jul 02 '18 at 13:27

14 Answers14

182

Why don't you just right click on the main() method and choose "Debug As... Java Application"?

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Dave Syer
  • 56,583
  • 10
  • 155
  • 143
103

There's section 19.2 in Spring Boot Reference that tells you about starting your application with remote debugging support enabled.

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
   -jar target/myproject-0.0.1-SNAPSHOT.jar

After you start your application just add that Remote Java Application configuration in Run/Debug configurations, select the port/address you defined when starting your app, and then you are free to debug.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
masster
  • 1,039
  • 1
  • 6
  • 8
  • This worked for me. Thanks! Eclipse couldn't debug my spring boot project (Kept complaining that the Main class couldn't be found). – user6123723 Oct 19 '14 at 21:00
  • 4
    Note that with Java 8 (I don't know about other versions) running `java -agentlib:jdwp=help` states "The older -Xrunjdwp interface can still be used, but will be removed in a future release". Instead use `-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n` – Paul Nov 16 '17 at 18:36
30

Easier solution:

Instead of typing mvn spring-boot:run, simply type mvnDebug spring-boot:run

You will still need to attach the debugger in Eclipse by making a new Debug Configuration for a "Remote Java Application" on the relevant port.

22

I didn't need to set up remote debugging in order to get this working, I used Maven.

  1. Ensure you have the Maven plugin installed into Eclipse.
  2. Click Run > Run Configurations > Maven Build > new launch configuration:
    • Base directory: browse to the root of your project
    • Goals: spring-boot::run.
  3. Click Apply then click Run.

NB. If your IDE has problems finding your project's source code when doing line-by-line debugging, take a look at this SO answer to find out how to manually attach your source code to the debug application.

Hope this helps someone!

Community
  • 1
  • 1
Dawngerpony
  • 3,288
  • 2
  • 34
  • 32
  • 1
    If you run the server with spring-boot:run, you won't be able to stop it by just pressing Red icon in console window. – Shailesh Pratapwar Apr 04 '18 at 06:33
  • I can stop it like so (may need netstat intalled, i forget if i dled a binary or came with Cygwin, or part of windows now): netstat -ano | grep "9999" --returns 36308 task id in Windows taskkill /F /PID 36308 – armyofda12mnkeys Jun 05 '20 at 15:22
21

How to debug a remote staging or production Spring Boot application

Server-side

Let's assume you have successfully followed Spring Boot's guide on setting up your Spring Boot application as a service. Your application artifact resides in /srv/my-app/my-app.war, accompanied by a configuration file /srv/my-app/my-app.conf:

# This is file my-app.conf
# What can you do in this .conf file? The my-app.war is prepended with a SysV init.d script
# (yes, take a look into the war file with a text editor). As my-app.war is symlinked in the init.d directory, that init.d script
# gets executed. One of its step is actually `source`ing this .conf file. Therefore we can do anything in this .conf file that
# we can also do in a regular shell script.

JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,address=localhost:8002,server=y,suspend=n"
export SPRING_PROFILES_ACTIVE=staging

When you restart your Spring Boot application with sudo service my-app restart, then in its log file located at /var/log/my-app.log should be a line saying Listening for transport dt_socket at address: 8002.

Client-side (developer machine)

Open an SSH port-forwarding tunnel to the server: ssh -L 8002:localhost:8002 myusername@staging.example.com. Keep this SSH session running.

In Eclipse, from the toolbar, select Run -> Debug Configurations -> select Remote Java Application -> click the New button -> select as Connection Type Standard (Socket Attach), as Host localhost, and as Port 8002 (or whatever you have configured in the steps before). Click Apply and then Debug.

The Eclipse debugger should now connect to the remote server. Switching to the Debug perspective should show the connected JVM and its threads. Breakpoints should fire as soon as they are remotely triggered.

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • Port forwarding and the tunneling was the bit I was missing. Ta. Crucial if you are debugging to an internet based server, say on Amazon. – PaulNUK Apr 10 '18 at 07:44
16

Run below command where pom.xml is placed:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

And start your remote java application with debugging option on port 5005

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
15

The best solution in my opinion is add a plugin in the pom.xml, and you don't need to do anything else all the time:

<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=9898
                    </jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
krmanish007
  • 6,749
  • 16
  • 58
  • 100
  • But then the command `mvn clean install` triggers the debugger and the build hangs. It would be cool if only the `mvnDebug clean install` command would do so. – Stephane Jul 08 '18 at 21:44
  • 2
    @Stephane, this configuration should probably be in a specific profile easy to enable – davidxxx Sep 16 '19 at 15:03
5
  • right click on your project and choose "Debug As => Java application or Spring boot App" -capture1 - enter image description here
  • If you want that your application pause in somewhere in your app double click on the left you will have like this capture 2. enter image description here
  • then when you start your app use those arrows to go next.(capture 3) enter image description here
  • This is a good answer to the title question. However, the description have a more specified and different question, which requires a different answer. – Thor Hovden Oct 19 '18 at 12:30
  • 1
    So I used to use MyEclispe and the Debug As -> Spring Boot App option was available and worked fine. I am not using Eclipse Oxygen and the Spring Boot App menu item is no longer available. Do I need to install a plugin for eclipse to get this? – Kachopsticks Jul 16 '20 at 17:24
3

This question is already answered, but i also got same issue to debug Springboot + gradle + jHipster,

Mostly Spring boot application can debug by right click and debug, but when you use gradle, having some additional environment parameter setup then it is not possible to debug directly.

To resolve this, Eclipse provided one additional features as Remote Java Application

by using this features you can debug your application.

Follow below step:

run your gradle application with ./gradlew bootRun --debug-jvm command

Now go to eclipse --> right click project and Debug configuration --> Remote Java Application.

add you host and port as localhost and port as 5005 (default for gradle debug, you can change it)

Refer for more detail and step.

bharatpatel
  • 1,203
  • 11
  • 22
3

Right click on the spring boot project -> debug as -> spring boot App. Put a debugger point and invoke the app from a client like postman

Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30
2

Please see http://java.dzone.com/articles/how-debug-remote-java-applicat to enable the remote debugging. If you are using tomcat to run your application, start tomcat with remote debug parameters or you can start tomcat with JPDA support by using following command.

Windows

<tomcat bin dir>/startup.bat jpda

*nix

<tomcat bin dir>/startup.sh jpda

this will enable remote debugging on port 8000

Sangram Jadhav
  • 2,438
  • 16
  • 17
2

With eclipse or any other IDE, just right click on your main spring boot application and click "debug as java application"

adia
  • 404
  • 2
  • 8
0

Right click on the Spring Boot Applications main class file -> select Debug As options -> Select Java Application

enter image description here Now you can use breakpoints to debug the application.

Bishal Jaiswal
  • 1,684
  • 13
  • 15
0

What's more convenient for me is usually:

  1. set up some JUnit test making a TestRestTemplate request on the server API(s) I wish to debug;
  2. set breakpoints on the server code I wish to debug;
  3. right click on the JUnit test name (in the editor window or JUnit view pane) -> Debug as -> JUnit test
  4. if you have more than one Run configuration, choose from the popup window that will appear the one you normally use to run your server in Eclipse, usually with Goals: clean spring-boot:run.

The Eclipse debugger will start the server and honor the breakpoints in the server code as well as the ones in the test code.

PJ_Finnegan
  • 1,981
  • 1
  • 20
  • 17