35

I have a Gradle project in Eclipse IDE and I usually use the option gradle run to run my Java application.

I have an error in my Java code and I want to debug it, but when I execute gradle run, the debugger doesn't stop in the breakpoints. In the "Debug as" menu, I don't have anything like gradle debug.

How can I debug my application?

3
  • 4
    Have you tried gradle run --debug-jvm ? Commented Oct 9, 2015 at 10:32
  • 2
    I will try it, but I wanted to debug using the eclipse IDE Commented Oct 9, 2015 at 11:26
  • There is a discussion on discuss.gradle.org/t/… on how to debug in eclipse using gradle tasks. Commented Oct 9, 2015 at 12:04

3 Answers 3

62

Even though the accepted answer should work, you can achieve it in a much easier way.


  1. Just run gradle run --debug-jvm. This starts the application in remote debug mode, and you can attach with any remote debugger, e.g., Eclipse, on port 5005.

  2. Assuming you use Eclipse as IDE: In Eclipse, go on your Project -> Debug as... -> Debug Configuration -> Remote Java Application. As host set localhost, as port 5005, and you are free to go.


For more information see the official Gradle Java plugin doc regarding testing.

[...] can also be enabled at invocation time via the --debug-jvm task option (since Gradle 1.12).

Sign up to request clarification or add additional context in comments.

6 Comments

this works, but not the accepted answer. The gradle one is the one actually works.
this works indeed. to change the default port, in the build.gradle file test section add like this: test { jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5099"] }
Is there a commandline switch to make this suspend when Eclipse connects, which doesn't require changing jvmArgs in build.gradle?
Here is the Gradle official guide. docs.gradle.org/current/userguide/…
If you have gradle in your project as a wrapper (and you likely don't have gradle installed by itself on your system) and you're using Eclipse: 1) Locate the gradlew file in your project 2) Open up a new terminal in Eclipse by going into the package explorer and right clicking the project containing gradlew > Show in Local Terminal > Terminal 3) Within the newly created terminal, enter gradlew run --debug-jvm 4) Perform step 2 in the answer above.
|
33

To debug a Gradle project in Eclipse follow these steps:

Step 1:
Put these in the build.gradle file:

tasks.withType(JavaExec) {
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9099'
    }
}

Step 2:

From the root of your project run:

gradle -DDEBUG=true run

You will now see something like this in the console:

Listening for transport dt_socket at address: 9099

Step 3:

Now set breakpoints in Eclipse in your source files.

Step 4:

As last step, now right-click in Eclipse on Project > Debug as > Debug configuration > Remote Java application.

There you have to set these fields:

1. Project (this should be set to name of your Eclipse project)
2. Host (here it's localhost)
3. Port (in this example it will be 9099)

Click "Debug". Now your program will stop whenever it hits a breakpoint in Eclipse.

To see details of these steps with an example project see this gradle example on GitHub:

5 Comments

Hey, when I try your steps, setting the debug configuration for remote application results in the error message Failed to connect to remote VM. Connection refused. I am trying to run a task that executes my cucumber tests from gradle, and stop at a breakpoint in the Java code. Any tips?
Hey @themathmagician, Im havent used cucumber tests but if its some kind of unit test you need to change the gradle function a little.Follow this example for unit tests: github.com/ayonious/gradle-tutorial/tree/master/…
This worked for me but I needed to change debug_socket to dt_socket. jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9099'
@Jerinaw thanks for mentioning it updating my answer
Xrunjdwp is for Java 1.4 or less
0

I wasn't able to run the gradle task in debug mode through adding the following code in build.gradle file.

tasks.withType(JavaExec) {
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
    }
}

Instead I did the following steps and managed to run it automatically in debug mode :

  1. All you need to to do is to go to $GRADLE_HOME/bin.
  2. Edit the file gradle or gradle.bat depending on your OS.
  3. Set the JVM parameter in the OS-specific way. For example:

gradle(UNIX) case :

DEFAULT_JVM_OPTS="-Xdebug-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

gradle.bat(NT) case :

set DEFAULT_JVM_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

. 4. Run the Gradle command from the console.

Comments

Your Answer

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.