Gradle "Could not initialize class org.codehaus.groovy.*" - Fix in CI
Gradle runs its build scripts on an embedded Groovy runtime. When you run Gradle on a JDK newer than that Gradle version supports, Groovy's reflection setup fails to initialize and the build dies before evaluating anything.
What this error means
The build fails very early with Could not initialize class org.codehaus.groovy.reflection.ReflectionCache (or ...vmplugin.v7.Java7), typically when an old Gradle runs on a new JDK (17/21).
FAILURE: Build failed with an exception.
* What went wrong:
Could not initialize class org.codehaus.groovy.reflection.ReflectionCache
* Try:
> Run with --stacktrace option to get the stack trace.Common causes
Gradle version too old for the JDK
Each Gradle release supports JDKs only up to a known version; running it on a newer JDK breaks Groovy reflection at init.
JAVA_HOME points at a too-new JDK
The runner provisioned JDK 21 but the wrapper pins an old Gradle, so Gradle itself runs on an unsupported JVM.
Strong encapsulation blocks reflective access
Newer JDKs deny the deep reflection old Groovy relies on without --add-opens, which old Gradle does not set.
How to fix it
Upgrade the Gradle wrapper
Use a Gradle version that supports your JDK (Gradle 8.5+ for JDK 21).
./gradlew wrapper --gradle-version 8.10
# then commit gradle/wrapper/gradle-wrapper.propertiesRun Gradle on a supported JDK
Point the Gradle launch JVM at a JDK the wrapper supports, while using toolchains to compile with a newer one.
# gradle.properties
org.gradle.java.home=/usr/lib/jvm/temurin-17Use a Java toolchain for compilation
Keep Gradle on a stable JDK and let toolchains pick the compile JDK.
java {
toolchain { languageVersion = JavaLanguageVersion.of(21) }
}How to prevent it
- Match the Gradle wrapper version to the runner JDK before upgrading either.
- Use Java toolchains so the build JVM and compile JVM are decoupled.
- Pin
gradle-wrapper.propertiesin the repo and bump it deliberately.