Resolving java.lang.IllegalAccessError in Lombok with OpenJDK 16

Question

What causes the java.lang.IllegalAccessError when using Lombok with OpenJDK 16?

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
    }

}

Answer

The java.lang.IllegalAccessError regarding Lombok and OpenJDK indicates an issue with access permissions between Lombok and Java's internal APIs, especially when using a newer Java version such as OpenJDK 16.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>16</source>
                <target>16</target>
                <compilerArgs>
                    <arg>--add-opens</arg>
                    <arg>jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

Causes

  • Lombok's reliance on internal Java APIs that were not exported by OpenJDK.
  • Java 16's stricter module system prevents Lombok from accessing certain internal classes.
  • Incompatibility between the Lombok version and the OpenJDK version.

Solutions

  • Ensure that you are using the latest stable version of Lombok compatible with your JDK.
  • Add necessary JVM arguments to allow access to internal APIs: `--add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED`
  • Upgrade or downgrade your JDK version if the specific combination of Lombok and your current JDK is known to have issues.

Common Mistakes

Mistake: Not using the latest version of Lombok.

Solution: Regularly check for updates in the Lombok repository and upgrade if necessary.

Mistake: Failing to add JVM arguments to access internal APIs.

Solution: Modify your project’s build configuration to include the necessary `--add-opens` argument.

Helpers

  • java.lang.IllegalAccessError
  • Lombok error OpenJDK 16
  • Lombok Java compatibility
  • fix IllegalAccessError Lombok
  • Java Lombok setup issues

Related Questions

⦿How to Compile Multiple Java Source Directories in a Single Maven Project

Learn how to configure Maven to compile multiple Java source directories in a single project with stepbystep guidance and examples.

⦿Why Doesn't the split() Method in Java Work with a Dot (.) as a Delimiter?

Learn why Javas String.split method fails to split using . and how to fix it in your code snippet.

⦿Understanding Why Java 8 Streams' .min() and .max() Compile with Static Methods from Integer

Explore why Java 8 Streams .min and .max compile when using static methods from Integer instead of a Comparator. Learn the details here.

⦿How to Handle Exceptions Thrown by a Thread in Java

Learn how to catch exceptions thrown from a thread in Java including code examples and common mistakes to avoid.

⦿How to Configure java.library.path for an Eclipse Project

Learn how to set the java.library.path in Eclipse for seamless integration of Java libraries that depend on OSspecific files like .dll .so or .jnilib.

⦿How to Determine the Source Location of a Java Class Loaded by the ClassLoader

Learn how to programmatically find where a Java ClassLoader loads a class from including handling class loading issues.

⦿How to Resolve java.lang.ClassNotFoundException for org.apache.commons.logging.LogFactory in Spring?

Learn how to fix the java.lang.ClassNotFoundException issue in Spring applications caused by missing org.apache.commons.logging.LogFactory.

⦿How to Properly Add JAR Files to a Spark Job Using spark-submit

Learn how to effectively add JAR files to a Spark job with sparksubmit including potential impacts on classpaths and distribution methods.

⦿How to Convert `int[]` to `Integer[]` in Java for Use in a Map

Learn how to convert int to Integer in Java allowing you to track frequency counts using a Map. See detailed code examples and solutions.

⦿How to Generate a Unique ID String in Java

Discover effective methods to create unique ID strings in Java including using UUIDs and alternatives.

© Copyright 2025 - CodingTechRoom.com