This project demonstrates a bug in Gradle which causes it to be unable to debug Java 9+ projects.
From the docs:
org.gradle.debug=(true,false)
When set to
true
, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
to the JVM command line and will suspend the virtual machine until a debugger is attached. Default isfalse
.
Gradle allows for remote debugging by running gradle --debug-jvm
. The --debug-jvm
flag instructs Gradle to supply the JVM with -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
. From Java 9 onwards, the argument is supposed to be -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
(reference). This is why remote debugging Java 9+ projects with Gradle's --debug-jvm
doesn't work.
Install Docker so that we can easily simulate the required environments.
- We'll first run the Java 9+ project to see that the debugger doesn't work.
- Start the debugger.
- It will fail with the error
java.io.IOException: handshake failed - connection prematurally closed
.
- Now we'll modify the project to show that it works in Java 8.
- Change line 10 of
build.gradle.kts
fromsourceCompatibility = JavaVersion.VERSION_13
tosourceCompatibility = JavaVersion.VERSION_1_8
- Change line 4 of
docker-compose.yml
fromimage: gradle:6.4.0-jdk14
toimage: gradle:6.4.0-jdk8
. - Start the debugger.
- The debugging session starts successfully.
- Change line 10 of
This project is under the MIT License.