Skip to content

Lombok IllegalAccessError with Java 16+

This article addresses the java.lang.IllegalAccessError that occurs when using Project Lombok with Java 16 and later versions. The error typically appears as:

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

Root Cause

The error occurs because Java 16+ implements stronger module access controls (JEP 396) that restrict access to internal JDK APIs. Lombok needs to access these internal compiler APIs to perform its bytecode manipulation, but the Java module system now blocks this access by default.

Primary Solution: Update Lombok Version

The simplest and recommended solution is to update to Lombok version 1.18.22 or later, which contains fixes for Java 16+ compatibility.

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

To find the latest version of Lombok, check Maven Central.

TIP

Always use the latest stable version of Lombok when working with newer Java versions to ensure compatibility.

Alternative Solution: Configure Maven Compiler

If you cannot immediately update Lombok, you can manually configure the Maven compiler plugin to open the necessary internal modules:

xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>16</source>
        <target>16</target>
        <fork>true</fork>
        <compilerArgs>
            <arg>--enable-preview</arg>
            <arg>-Xlint:all</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED</arg>
        </compilerArgs>
        <encoding>UTF-8</encoding>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.16</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

WARNING

This workaround is complex and may not be necessary with newer Lombok versions. Always prefer updating Lombok over manual configuration.

Manual Implementation Alternative

If Lombok compatibility issues persist, you can manually implement the functionality provided by Lombok annotations. The @Data annotation is equivalent to:

java
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@EqualsAndHashCode
public class Ingredient {
    private final String id;
    private final String name;
    private final Type type;

    public enum Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }
}

You would need to manually write the getters, setters, constructors, and other boilerplate code that Lombok generates automatically.

Best Practices

  1. Keep Lombok updated - Always use the latest version compatible with your Java version
  2. Check compatibility - Verify Lombok compatibility before upgrading Java versions
  3. Consider alternatives - For new projects, evaluate if records (Java 14+) can replace some Lombok use cases
  4. Use provided scope - Lombok should typically be marked as provided scope since it's only needed at compile time

INFO

Java records (introduced in Java 14) can serve as an alternative to Lombok's @Data for simple data carrier classes, though they don't replace all Lombok functionality.

Conclusion

The IllegalAccessError with Lombok and Java 16+ is resolved by updating to Lombok 1.18.22 or later. For ongoing projects, regularly update Lombok to ensure compatibility with newer Java versions. The manual Maven configuration workaround should only be used temporarily if version updates aren't immediately feasible.