How to Fix Gradle Compilation Errors in Java
In this tutorial, you'll learn about How to Fix Gradle Compilation Errors in Java. We cover key concepts, practical examples, and best practices.
The Problem
Your Gradle build fails with Could not determine the dependencies of task ':compileJava', Unsupported class file major version, or Daemon will be stopped at the end of the build. Build failures prevent code changes from being tested.
Quick Fix
Fix 1: Java Version Mismatch
WRONG â Gradle uses the wrong JDK version:
./gradlew compileJava
# error: invalid source release: 17
# (the JDK is too old for source 17)
RIGHT â set the Java version in build.gradle:
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Or set via Gradle toolchain:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
Check the JDK version:
java -version
# openjdk version "17.0.9" 2026-10-17 LTS
Fix 2: Unsupported Class File Major Version
# * What went wrong:
# Unable to load class 'org.gradle.internal.compiler.java.ClassNameCollector'.
# Unsupported class file major version 65
# (Gradle version is too old for the JDK)
RIGHT â upgrade Gradle or use a compatible JDK:
./gradlew wrapper --gradle-version 8.5
# (upgrades the Gradle wrapper to 8.5 which supports JDK 21)
Fix 3: Dependency Resolution Failure
# Could not resolve all files for configuration ':compileClasspath'.
# Could not find com.google.guava:guava:32.1.2-jre.
WRONG â missing repository:
dependencies {
implementation 'com.google.guava:guava:32.1.2-jre'
// (repository not specified)
}
RIGHT â add the repository:
repositories {
mavenCentral()
// or maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'com.google.guava:guava:32.1.2-jre'
}
Fix 4: Gradle Daemon Memory Issues
# Daemon will be stopped at the end of the build
# Out of memory: Java heap space
RIGHT â increase daemon memory in gradle.properties:
org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
Fix 5: Kotlin DSL Syntax Errors
WRONG â mixing Groovy and Kotlin syntax:
// build.gradle.kts
plugins {
id("java")
id 'application' // Groovy syntax in Kotlin DSL
}
RIGHT â use consistent Kotlin DSL syntax:
plugins {
java
application
}
Fix 6: Force Clean Build
# Stale cache causes errors:
./gradlew clean build --refresh-dependencies
./gradlew build
# (removes build directory and clears caches)
Use DodaTech's Build Compiler to analyze Gradle configurations, detect dependency conflicts, and optimize build performance for Java projects.
Prevention
- Use Gradle wrapper (
gradlew) for consistent builds across machines. - Set
sourceCompatibilityandtargetCompatibilityin all projects. - Keep Gradle and JDK versions compatible.
- Run
./gradlew --stopto restart the daemon when switching projects. - Use
--build-cachefor faster incremental builds.
Common Mistakes with gradle compile
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world JAVA code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro