Java
Lombok
Error
Javac
IllegalAccessError

java.lang.IllegalAccessError class lombok.javac.apt.LombokProcessor cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Before exploring the technical underpinnings of the java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment, it's crucial to understand the primary components involved and the typical scenarios where this error might arise. This article aims to provide a comprehensive understanding of this error and guide you on how to resolve it when encountered.

Understanding the Components

Lombok

Lombok is a popular Java library that offers annotations to reduce the boilerplate code frequently used in Java applications. It uses annotation processing at compile time to generate code, such as getters, setters, equals, hashcode, and other utility methods.

Javac Processing Environment

com.sun.tools.javac.processing.JavacProcessingEnvironment is a part of the Java Compiler (Javac) API, which provides tools for annotation processors to interact with and modify the data during the compilation process.

Error Explanation

The java.lang.IllegalAccessError occurs when there's an attempt to access or modify a class that is not accessible from the location trying to access it. In this specific error, the LombokProcessor lacks the necessary permissions to interact with JavacProcessingEnvironment.

Technical Explanation

This error typically arises due to:

  1. Module System Restrictions: With the introduction of Java's module system (Project Jigsaw) in Java 9, accessibility and encapsulation at the module level have been strictly enforced. If the module containing JavacProcessingEnvironment doesn't export it, classes outside the module cannot access it.
  2. Version Compatibility Issues: There may be an incompatibility between the versions of Java, Lombok, or other libraries used in the project, leading to accessibility issues during compilation.
  3. Class-loader Issues: Sometimes, different class-loaders might load the same class, causing conflicts that lead to IllegalAccessError.

Resolution Steps

To resolve this error, follow the steps below:

Step 1: Validate Java and Lombok Version Compatibility

Ensure that the versions of Java and Lombok are compatible. Update both Java and Lombok to the latest versions if needed. Check the Lombok release notes and Java compatibility to confirm compatibility.

Step 2: Module Exports

If you're using Java 9 or above, you can modify the command-line options to allow access to internal APIs. Use --add-exports to make JavacProcessingEnvironment accessible:

bash
javac --add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED -cp . your_application.java

Step 3: Check for Duplicate Libraries

Ensure there are no duplicate or conflicting versions of libraries in your project. Use a build tool like Maven or Gradle to manage dependencies effectively.

Step 4: Update Project Configuration

Ideally, update your IDE settings or build configuration to ensure that Lombok is correctly configured. Ensure annotation processing is enabled, and the correct Lombok plugin and dependencies are added.

Example Scenario

Consider a scenario where a project is running on JDK 11 and Lombok 1.18.2. Here's a potential setup in a Maven pom.xml:

xml
1<dependency>
2  <groupId>org.projectlombok</groupId>
3  <artifactId>lombok</artifactId>
4  <version>1.18.2</version>
5  <scope>provided</scope>
6</dependency>

To check if there are compatibility issues, update the Pom to the latest Lombok version, e.g.,:

xml
<version>1.18.28</version>

Summary Table

IssuePossible CauseResolution
IllegalAccessErrorModule system restrictionsUse --add-exports option
IllegalAccessErrorVersion compatibilityUpdate Java and Lombok versions
IllegalAccessErrorDuplicate librariesUse Maven/Gradle to manage dependencies
IllegalAccessErrorAnnotation processor misconfigurationCorrect Lombok setup in the project

Additional Considerations

While combating the java.lang.IllegalAccessError, consider the larger context of your application:

  • Maintain a consistent build environment: Use Docker or tools like SDKMAN! to ensure consistent Java environments across the team.
  • Monitor Lombok changes and announcements: Address potential deprecations or changes in how lombok interacts with Java's native modules.

By addressing the factors outlined above, you can effectively deal with the java.lang.IllegalAccessError and ensure a smooth build process with Lombok and Java.


Course illustration
Course illustration

All Rights Reserved.