Debugging a Java program by running "gradle run"

I have a Gradle project in the Eclipse IDE, and I usually use the gradle run option to launch 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 does not stop at breakpoints. On the Debug As menu, I have nothing like gradle debug .

How to debug an application?

+16
java debugging eclipse gradle
source share
4 answers

To debug a Gradle project in Eclipse, follow these steps:

Step 1 :
Put this 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 

Now you will 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 a last step, now right-click in Eclipse under Project> Debug as> Debug configuration> Remote Java application.

There you must set these fields:

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

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

To see the details of these steps with an example project, see the following example on GitHub :

+23
source share

Although the accepted answer should work, you can achieve it much easier.


  1. Just run gradle run --debug-jvm . This launches the application in remote debugging mode, and you can connect to any remote debugger, for example, Eclipse, to port 5005 .

  2. Assuming you are using Eclipse as an IDE: in Eclipse, go to your project -> Debug as ... -> Debug Configuration -> Remote Java Application. Set the host as localhost as port 5005 and you can go free.


For more information, see the Gradle Java Plugin white paper on testing.

[...] can also be enabled during a call through the task option --debug-jvm (since version 1.12).

+21
source share

If you are looking for an answer to debug your Spring Boot web application - Gradle .. then this is how I debugged

 Step 1: build gradle peoject > gradle build Step 2: From command prompt > C:\my jar or war file is in this folder>java -Xdebug -agentlib:jdwp=transport=dt_socket,address=9999,server=y,suspend=y -jar myJarOrWarApplication-1.0.0.war Step 3: Go to eclipse --> right click on your project --> Debug as --> Debug configurations --> Remote Java Application --> Create new configuration --> change port to 9999 -->hit Debug button Step 4: Place debug points in your code in eclipse. Step 5: Call your application. 
0
source share

I was unable to run the gradle task in debug mode by adding the following code to the 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 took the following steps and managed to run it automatically in debug mode:

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

gradle case (UNIX):

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

case gradle.bat (NT):

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

4. Run the Gradle command from the console.

0
source share

All Articles