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