Keywords: Lombok | IllegalAccessError | OpenJDK 16 | Module System | Maven
Abstract: This article provides an in-depth analysis of the common IllegalAccessError encountered when using the Lombok plugin in Java development, particularly with OpenJDK 16 and later versions due to module access restrictions. By examining the root cause and comparing different solutions, it details how to resolve the issue by upgrading Lombok to version 1.18.22 or higher. With practical code examples and Maven configurations, the article offers step-by-step fixes and best practices to help developers quickly address similar modularization-related compilation errors.
Problem Background and Error Analysis
In Java development, Lombok is a popular library that reduces boilerplate code by automatically generating methods like getters and setters through annotations. However, with the introduction of the Java module system, especially in OpenJDK 16 and above, developers may encounter the following error:
java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x3b67ef9b) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x3b67ef9b
The root cause of this error lies in the access control mechanisms of the Java module system. Starting from JDK 9, Java introduced modularization, encapsulating internal JDK APIs (e.g., com.sun.tools.javac.processing.JavacProcessingEnvironment) within specific modules. By default, these internal APIs are not accessible to unnamed modules (i.e., the traditional classpath), and Lombok requires access to these APIs during compilation for annotation processing.
Error Reproduction and Code Example
Below is a typical Lombok usage scenario employing the @Data annotation to auto-generate methods:
import lombok.Data;
@Data
public class Ingredient {
private final String id;
private final String name;
private final Type type;
public enum Type {
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}
In a Maven project, if the Lombok dependency version is too old (e.g., 1.18.12) and OpenJDK 16 is used for compilation, the IllegalAccessError is triggered because older Lombok versions do not adapt to the new module access rules.
Solution: Upgrading Lombok Version
According to official fixes, Lombok version 1.18.22 and later resolve this module access issue. To upgrade, explicitly specify the Lombok version in the Maven pom.xml file:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
After upgrading, Lombok correctly handles module permissions, avoiding errors when accessing internal APIs. Developers are advised to regularly check and update to the latest stable version for compatibility with new JDK features.
In-Depth Principles: Module System and Access Control
The Java module system uses module-info.java to define module exports and dependencies. The jdk.compiler module does not export the com.sun.tools.javac.processing package by default, preventing Lombok in unnamed modules from accessing it. Newer Lombok versions circumvent this by using standard APIs or requesting module access, rather than relying directly on internal implementations.
Alternative Solutions and Limitations
Besides upgrading Lombok, developers might attempt to force package exports via command-line arguments, such as:
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
However, this approach is intrusive, may affect project portability, and is not recommended for production environments. In contrast, upgrading Lombok is a safer and more sustainable solution.
Practical Recommendations and Summary
To avoid similar issues, it is recommended to:
- Always use a Lombok version compatible with the JDK version.
- Explicitly manage dependency versions in Maven or Gradle to prevent implicit conflicts.
- Regularly monitor Lombok official updates and apply fixes promptly.
Through the analysis and solutions provided in this article, developers can quickly identify and resolve module access errors with Lombok in OpenJDK 16 and later, enhancing development efficiency.