Skip to content

How to Fix Maven Compilation Errors in Java

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix Maven Compilation Errors in Java. We cover key concepts, practical examples, and best practices.

The Problem

You run mvn compile and get Source option 5 is no longer supported, cannot find symbol, or package org.junit does not exist. The build fails before running any tests, blocking development.

Quick Fix

Fix 1: Java Version Mismatch

WRONG — pom.xml specifies an older Java version than the installed JDK:

mvn compile
# [ERROR] Source option 5 is no longer supported. Use 7 or later.

RIGHT — set the correct Java version in pom.xml:

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>

Or use the compiler plugin explicitly:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>17</source>
        <target>17</target>
    </configuration>
</plugin>

Fix 2: Missing Dependencies

WRONG — using a class from a library not in pom.xml:

import com.google.gson.Gson;
// cannot find symbol: class Gson

RIGHT — add the dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

Then update the dependency tree:

mvn dependency:resolve
# (downloads the missing artifact)

Fix 3: Module-Info Conflicts (Java 9+)

# [ERROR] module java.base does not "opens java.lang" to unnamed module

RIGHT — add JVM args to the compiler plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <arg>--add-opens</arg>
            <arg>java.base/java.lang=ALL-UNNAMED</arg>
        </compilerArgs>
    </configuration>
</plugin>

Fix 4: Plugin Version Too Old

# [ERROR] Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 not found

RIGHT — specify a recent plugin version:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
</plugin>

Fix 5: Lombok Annotation Processing

# [ERROR] cannot find symbol: log (Lombok @Slf4j)

RIGHT — add Lombok as a compile-time dependency and annotation processor:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>

Also enable annotation processing:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

Fix 6: Clean Build Cache

# Stale compiled classes cause strange errors:
mvn clean compile
# (removes target/ and recompiles everything)

Use DodaTech's Build Analyzer to trace dependency graphs, detect version conflicts, and optimize Maven build times in your CI pipeline.

Prevention

  • Set maven.compiler.source and target to match the JDK version.
  • Run mvn dependency:tree to detect version conflicts.
  • Use a Maven wrapper (mvnw) for consistent builds.
  • Run mvn clean before compiling after changing dependencies.
  • Keep Maven and plugins updated.

Common Mistakes with maven compile

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

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

### Why does Maven say "Source option 5 is no longer supported"?

Java 11+ removed support for compiling with source/target versions below 7. Update maven.compiler.source and maven.compiler.target to 11 or higher in your pom.xml. Java 17 is the current LTS and recommended for new projects.

How do I see the full dependency tree?

Run mvn dependency:tree -Dverbose to see the complete dependency hierarchy, including transitive dependencies and version conflict resolution. This helps identify which version of a library is actually used.

What is the difference between compile, provided, and runtime scope?

compile scope is available everywhere (default). provided scope is available at compile time but provided by the runtime environment (e.g., servlet API in a web container). runtime scope is not needed for compilation but is needed at runtime.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro