Reputation: 465
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?
Upvotes: 35
Views: 60956
Reputation: 1
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 :
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.
Upvotes: 0
Reputation: 2200
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:
Upvotes: 33
Reputation: 12668
Even though the accepted answer should work, you can achieve it in a much easier way.
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
.
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).
Upvotes: 61