The java.lang.IllegalAccessError
with the message “class lombok.javac.apt.LombokProcessor cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment” occurs when the Lombok library cannot access the internal Java Compiler (javac) classes due to a change in the visibility of those classes in newer JDK versions.
Starting from JDK 9, the Java Platform Module System (JPMS) was introduced, which enforces stronger encapsulation by default. This means that some of the internal Java classes, such as com.sun.tools.javac.processing.JavacProcessingEnvironment
, are no longer accessible by default.
To resolve this issue, you can follow these steps:
- Update the Lombok library: Make sure you are using the latest version of Lombok, as newer versions might have better support for the latest JDK releases. Update the Lombok dependency in your build tool (Maven, Gradle, etc.) to the latest version. For example, with Maven, you would update the version in your
pom.xml
:org.projectlombok lombok 1.18.22 provided
Replace 1.18.22 with the latest version available. - Add required JVM arguments: You may need to add the following JVM arguments to your build tool to allow Lombok to access the required internal classes
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
For example, in Maven, you can add these arguments to the maven-compiler-plugin configuration in your pom.xml:org.apache.maven.plugins maven-compiler-plugin 3.8.1 11 11 --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED ... --add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED ```
In Gradle, you can add these arguments to the compileJava task in your build.gradle:compileJava { options.compilerArgs.addAll([ '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', '--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED' ]) }
After updating the Lombok library and adding the required JVM arguments, the java.lang.IllegalAccessError should be resolved, allowing your project to compile and run without issues. If you still encounter problems, consider reaching out to the Lombok community for additional support, as the issue might be related to a specific Lombok feature or usage scenario.