Skip to content

How to Fix Gradle Compilation Errors in Java

DodaTech Updated 2026-06-24 3 min read

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 sourceCompatibility and targetCompatibility in all projects.
  • Keep Gradle and JDK versions compatible.
  • Run ./gradlew --stop to restart the daemon when switching projects.
  • Use --build-cache for faster incremental builds.

Common Mistakes with gradle compile

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. 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

### What is the difference between implementation, api, and compileOnly?

implementation exposes dependencies only to the module (not consumers). api exposes them to consumers. compileOnly is for compile-time only (like Lombok). Use implementation by default and api only when the type appears in public APIs.

Why does Gradle say "Daemon will be stopped at the end of the build"?

The Gradle daemon ran out of memory during the build. Increase org.gradle.jvmargs in gradle.properties. The default is 512MB. Set it to 2048m for medium-sized projects and 4096m for large projects.

How do I see the dependency tree in Gradle?

Run ./gradlew dependencies --configuration compileClasspath to see all compile-time dependencies with their transitive versions. Add --scan for a detailed build scan in the browser.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro