java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment [duplicate]
Asked Answered
B

3

216

I am somewhat new to coding and am trying to use the Lombok plugin to automatically create Getters/Setters e.t.c. for my fields of a specific class. In doing so I get greeted with the following error:

The error:

java: 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

After doing some searching online, I found out that this error is related to an issue in OpenJDK 15 but I am currently using OpenJDK 16, hence why I am confused that I am still getting this error.

This thread claims to have a solution: https://github.com/rzwitserloot/lombok/issues/2681#issuecomment-748616687 but after implementing the plugin, it does not seem to make any difference and I still receive the error.

I have most likely made a trivial mistake since I am a beginner but if someone knows what I am missing please let me know.

Class Using @Data (Lombok):

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
    }

}

My Pom File:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>sia</groupId>
    <artifactId>taco-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>taco-cloud</name>
    <description>Taco Cloud Project</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Backus answered 25/3, 2021 at 14:10 Comment(8)
If you are on JDK16 why not using records? If you are a beginner why not using the fundamentals to learn how they work? Lombok is known not working correctly in the published version on JDK16... so you have to wait either a newer version is available or just use plain java ... (IDE can help here a lot...) ....Willms
BTW: can you tell where you found that: .. is related to an issue in OpenJDK 15..?Willms
@Willms I thought that it was an issue with OpenJDK 15 since this post signified that in the release version of OpenJDK 16 the problem would no longer be present: github.com/rzwitserloot/lombok/issues/… but looking back, I think he was simply referring to a beta build of OpenJDK 16?Backus
Yes JDK16 has closed things which had been left open before (internal things in the JDK) which are being used by Lombok etc. and it was clear for a longer time that the closing will happen....Willms
@Willms I see, thanks for the info. Is there a workaround to still use Lombok in JDK 16 or should I switch to JDK 15 or earlier?Backus
@Willms As a heads-up, I have opted to remove the Lombok dependency and instead made my Getters, Setters, e.t.c. manually in the code. Got my application to run successfully after that.Backus
I was getting a similar error doing a maven clean install on mac with open jdk 13. I downgraded the java version to 11 and was able to build the project with no errors. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project phms-domain: Fatal error compiling: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (Imprisonment
In my simple maven project, I am not using lombok still I am getting above error. In IntelliJ I am using another separate Gradle project with lombok. Could that be the cause?Neill
R
510

Switch to at least the 1.18.22 version of Lombok that contains the fix

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

To see the last version of lombok, follow this link on search.maven.org

Rabinowitz answered 7/4, 2021 at 7:8 Comment(12)
I am still getting the error with this version (1.18.20) and Java 16. It disappears when I downgrade to Java 15.Lobachevsky
In my case I've to update the spring boot version 2.4.2 > 2.5.1Merkle
Worked for me, I'm using Java 16Japhetic
I have this problem on IntelliJ community 2021.2 version that do not have any spring boot initializer... How I can fix that?Tenorite
This works for me in Java 17 .. thanks.Foreland
Woks under openjdk 11Penumbra
Worked for me, openjdk version "11.0.13" 2021-10-19 LTSMeunier
Thanks. It got fixed after upgrading lombok to 1.18.22 . (on JDK 16)Ashley
I was using lombok-1.18.24.jar in Eclipse. In pom, existing version was 1.18.16. Updated to 1.18.24. Worked for me.Piderit
This worked for me. JAVA_HOME version is 18.0.1.1 (vscode java.jdt.ls.java.home=jdk-18.0.1.1), Java version of the Maven Project is jdk1.8.0_341.Lapsus
In my case I downgraded java version from 17 to 11 and it worked fine!Feliciafeliciano
In my case, I was facing this issue after upgrading AGP 7.4.2 and this fix resolved the error. Thank youGyratory
P
12

It seems can be solved by adding plugin in pom.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>
        <!--                    <release>16</release>-->
        <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>
        <!--for unmappable characters in classes-->
        <encoding>UTF-8</encoding>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
        <!--for lombok annotations to resolve-->
        <!--contradictory to maven, intelliJ fails with this-->
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.16</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

solution from Lombok's access to jdk.compiler's internal packages incompatible with Java-16

Pegeen answered 30/3, 2021 at 3:8 Comment(1)
I have attempted to implement this, but receive the following error: java: invalid source release 15 with --enable-preview (preview language features are only supported for release 16) I then attempted to use release 16 but then received the original error.Backus
B
4

@FrankyFred has provided a temporary solution to be able to use Lombok: https://mcmap.net/q/125520/-java-lang-illegalaccesserror-class-lombok-javac-apt-lombokprocessor-cannot-access-class-com-sun-tools-javac-processing-javacprocessingenvironment-duplicate


As far as my research goes and the various responses that I received to this question, it seems like there is no possible way of running Lombok currently in OpenJDK 16. As such, an alternative to using Lombok is to put the @data methods manually in your code. The following link briefly explains how to do this in case you are new to this topic: https://javabydeveloper.com/lombok-data-annotation/#:~:text=Lombok%20Data%20annotation%20(%20%40Data%20),as%20well%20as%20a%20constructor.

Sample pseudo code representing what @Data really represents:

@Getter @Setter 
@RequiredArgsConstructor
@ToString 
@EqualsAndHashCode
public class User1 {
  private Long id;
  private String username;
  
}

The Lombok @Data annotation minimizes the usage of more annotations when you need to generate all the above seven requirements, but they essentially achieve the same objectives when running the code.

I am by no means an expert at this, but hopefully, this will help anyone else who gets stuck using Lombok but needs to find an alternative since their book/tutorial might utilize it and u would still like to continue with the book/tutorial.

Backus answered 27/3, 2021 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.