Gradle "Could not initialize class org.codehaus.groovy..." - Fix JDK
Gradle’s embedded Groovy could not initialize because the JDK running Gradle is newer than that Gradle version supports. Strong module encapsulation in newer JDKs breaks the reflection Groovy relies on.
What this error means
Gradle fails during configuration with Could not initialize class org.codehaus.groovy.reflection.ReflectionCache (often an InaccessibleObjectException underneath). It happens before your build logic, on a too-new JDK.
* What went wrong:
Could not initialize class org.codehaus.groovy.reflection.ReflectionCache
> Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make
... accessible: module java.base does not "opens java.lang" to unnamed moduleCommon causes
Gradle version too old for the JDK
Older Gradle bundles an older Groovy that uses deep reflection. Newer JDKs enforce module encapsulation, so Groovy cannot initialize.
JAVA_HOME points at a too-new JDK
CI provisioned the newest JDK and Gradle picked it up to run on. The Gradle-runtime JDK, not your build target, is the problem.
How to fix it
Upgrade the Gradle wrapper
A newer Gradle bundles a Groovy compatible with current JDKs.
./gradlew wrapper --gradle-version 8.8
# commit the updated gradle-wrapper.propertiesOr run Gradle on a supported JDK
Keep your code targeting newer Java via toolchains, but run the Gradle process on a JDK this Gradle supports.
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17' # JDK that runs GradleHow to prevent it
- Keep the Gradle wrapper current with the JDKs CI provisions.
- Decouple the compile JDK from the Gradle-runtime JDK with toolchains.
- Commit gradle-wrapper.properties so everyone runs the same Gradle.