[Solved] NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree $JCImport

When rebuilding a Java or Spring application, after upgrading Java 21, you may face the issue “NoSuchFieldError: Class JCTree$JCImport does not have member field”.

exceptions-notes

When rebuilding a Java or Spring application after upgrading to Java 21, you may encounter the issue “NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field” during the build process. Let’s learn how to fix it.

1. The Error

When building the project after the JDK 21 upgrade, the build error looks like this:

java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport 
  does not have member field 'com.sun.tools.javac.tree.JCTree qualid'
  ...
  ...

2. The Root Cause

The com.sun.tools.javac.tree.JCTree$JCImport class, including its ‘qualid’ field, is part of the internal implementation of the Java compiler (javac). It stores the internal representation of Java source code in the Abstract Syntax Tree (AST) form. It’s not intended for general use in regular programming tasks.

To provide its functionality using annotations, Lombok accesses this internal JDK API (com.sun.tools.javac.tree.JCTree$JCImport) with reflection access to its field ‘qualid’. [Link]

The field has been changed in Java 21. Till Java 20, it was of type JCTree. In Java 21, its type has been changed to JCFieldAccess. [Issue-3393]

public JCTree qualid;  //Java 20 and before

public JCFieldAccess qualid;  //Java 21 and Later

The corresponding changes have been done in ‘Lombok 1.18.30‘ [Change log].

3. The Solution

To solve this error, we need to upgrade the Lombok version to 1.18.30 or the latest available version. The issue has been fixed in this version.

The recommendation is to always update to the latest version of Lombok.

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.32</version>
  <scope>provided</scope>
</dependency>
dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.32'
}

Do not forget to update the version in maven-compiler-plugin also if you have added it to the project.

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
      <source>22</source>
      <target>22</target>
      <enablePreview>true</enablePreview>
      <annotationProcessorPaths>
        <path>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.32</version>
        </path>

        //...
        </annotationProcessorPaths>
      <compilerArgs>--enable-preview</compilerArgs>
    </configuration>
  </plugin>
</plugins>
plugins {
    id 'java'
}

dependencies {
    annotationProcessor 'org.projectlombok:lombok:1.18.32'
    implementation 'org.projectlombok:lombok:1.18.32'
}

4. Conclusion

This short Java tutorial discussed the ‘NoSuchFieldError: Class JCTree$JCImport does not have member field’ caused by Lombok and how to fix it by updating the Lombok version in dependencies file.

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.