62

I am working on spring app and need to step through a controller method to see how it works. I am working in eclipse and building my app with gradle bootRun command.

How to run it in debug mode?

i tried gradle bootRun --debug but it's just debug log, not debug mode

i tried gradle help --task bootRun -- this gives me info about task there i saw --debug-jvm option but when i run gradle bootRun --debug-jvm application doesn't start

rigby
  • 1,280
  • 3
  • 13
  • 21
  • Have you had a look at [Spring Logging](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html) yet? That would be the best way to do this. – px06 Sep 14 '16 at 12:53
  • Cain you detail 'application doesn't start" ? Is there an exception that you get ? – Alpar Sep 14 '16 at 12:53
  • @alpar the site doesn't show in the browser. i see This site can’t be reached message – rigby Sep 14 '16 at 13:04
  • If you're using Eclipse, why are you using Gradle to run the app? Eclipse is the IDE, not Gradle... – E-Riz Sep 14 '16 at 13:53

7 Answers7

104

After you run gradle bootRun --debug-jvm the application is suspended until you connect your debugger to the port it is listening on (port 5005).

dankirkd
  • 1,739
  • 1
  • 15
  • 8
  • having an issue running gradle bootRun --debug-jvm. hangs at Building 96% > ::bootRun – Pete_ch Feb 02 '17 at 01:19
  • 2
    this is the right answer, **suspended** means you need to click your **remote debug** button to connect to listener and then app start :P – Neal.Shan Aug 20 '17 at 09:38
  • 7
    By default, the debug port is 5005. How to start from a custom port say 5006? – hariharan kumar Dec 19 '17 at 02:55
  • 6
    Also, how to start without suspend? – AlikElzin-kilaka Feb 21 '18 at 01:12
  • 1
    @hariharankumar I haven't tested with `bootRun` specifically, but the `BootRun` task extends `JavaExec` (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java), and I made it work with `JavaExec` passing `jvmArgs` to the task, so you could apply a similar logic: https://stackoverflow.com/a/53946140/4850646 – Lucas Basquerotto Dec 27 '18 at 13:53
44

As a response to dankdirkd's answer above: (compare)

gradle bootRun --debug-jvm

will make the gradle build run in debug mode. That probably is not what you want. What you want to achieve is that the springBoot task starts your application in debug mode.

The spring boot task extends the gradle JavaExec task. You can configure the bootRun task in your build.gradle file to add a debug configuration like this:

bootRun {
  jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32323"]
}

For the build.gradle.kts this would look like this (with suspend mode disabled):

tasks {
    val bootRun by getting(BootRun::class) {
        jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=32323")
    }
}

If your server is within a cloud and you want to debug from local machine, you need to make sure that it allows connections from outside. Use below configuration in that case

tasks {
val bootRun by getting(BootRun::class) {
    jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
    }
}

Note that the address is now 0.0.0.0:port instead of just port

Anand Vaidya
  • 1,374
  • 11
  • 26
rexford
  • 5,192
  • 5
  • 27
  • 40
  • 7
    I don't believe that --debug-jvm debugs the main gradle process. The bootRun process is a subclass of JavaExec which starts a child process in debug mode when receiving the --debug-jvm flag. https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:jvmArgs – Jazzepi Jul 31 '18 at 02:13
  • So i just add bootRun { jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32323"] } into the build.gradle. And that's it? – Nick Wills Dec 02 '21 at 16:57
6

For people hitting this via Google and wondering how to enable Spring's debug mode (normally done by java -jar app.jar --debug) and using Gradle, here is how. This passes --debug to the main class which is how you turn on Spring Boot's debug mode which logs autoconfig classes among other things.

./gradlew bootRun --args='--debug'
Captain Man
  • 6,997
  • 6
  • 48
  • 74
5

For build.gradle.kts file you can also simply use below:

tasks.withType<BootRun> {
    jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
}
SSP
  • 2,650
  • 5
  • 31
  • 49
Himadri
  • 93
  • 1
  • 2
4

I personally prefer going under Gradle tasks and right-clicking on the bootRun. This is useful in the IDE compared to the terminal.

kgui
  • 4,015
  • 5
  • 41
  • 53
  • 1
    But how do u use Gradle tasks to run an application in debug mode? I also don't like the idea of using temrinal – Nick Wills Dec 02 '21 at 16:56
0

For those looking for IntelliJ example with gradle bootRun task:

  1. Add following bootRun task definition to build.gradle file
 bootRun {
   jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
 }
  1. Run bootRun task
  2. Once you get to the point where console/terminal says "Listening for transport dt_socket at address: 5005" go to "Run --> Attach to Process..." and choose your app
  3. Add breakpoints anywhere you want
lord5et
  • 422
  • 7
  • 6
-1

Define an executes a Java application in a child process.

task executeApp() {
    doFirst {
       println "Executing java app from Gradle..."
       javaexec {
           main = "com.mymain"
           jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=30000"]
       }
    }
}

Set your breakpoints in the java code. After execute the Gradle task.For example in Windows:

  .\gradlew.bat executeApp

The task waits until you attach the debugger. For example in Netbeans go to Debug->Attach debugger , set 30000 on port Field.

jsci
  • 11
  • Thanks. This indeed can allow developer to leaverage on breakpoint and trace thru codes. However, is there a way to combine both steps (run apps and attach debugger) Ina single execution of command or a button click. Cos Everytime when we make java code change, we have to stop the above process. Then execute these 2 steps manually. Seem like a chores to me. – Nick Wills Mar 31 '20 at 01:21