How to Fix Maven Compilation Errors in Java
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.sourceandtargetto match the JDK version. - Run
mvn dependency:treeto detect version conflicts. - Use a Maven wrapper (
mvnw) for consistent builds. - Run
mvn cleanbefore compiling after changing dependencies. - Keep Maven and plugins updated.
Common Mistakes with maven compile
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro