Search code examples
javacompilationupgradelombokmapstruct

why compile error when upgrade lombok to 1.18.28 with jdk17?


I'm upgrading the Java version to 17. Here is the stacktrace.

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module @2eb9b035
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:354)
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:297)
    at java.lang.reflect.Field.checkCanSetAccessible (Field.java:178)
    at java.lang.reflect.Field.setAccessible (Field.java:172)
    at lombok.permit.Permit.setAccessible (Permit.java:71)
    at lombok.permit.Permit.getField (Permit.java:105)
    at lombok.javac.apt.LombokProcessor.getFieldAccessor (LombokProcessor.java:115)
    at lombok.javac.apt.LombokProcessor.<clinit> (LombokProcessor.java:109)
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstanceWithCaller (Constructor.java:499)
    at java.lang.reflect.ReflectAccess.newInstance (ReflectAccess.java:128)
    at jdk.internal.reflect.ReflectionFactory.newInstance (ReflectionFactory.java:347)
    at java.lang.Class.newInstance (Class.java:645)
    at lombok.core.AnnotationProcessor$JavacDescriptor.want (AnnotationProcessor.java:115)
    at lombok.core.AnnotationProcessor.init (AnnotationProcessor.java:177)
    at lombok.launch.AnnotationProcessorHider$AnnotationProcessor.init (AnnotationProcessor.java:73)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init> (JavacProcessingEnvironment.java:701)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next (JavacProcessingEnvironment.java:828)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs (JavacProcessingEnvironment.java:924)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run (JavacProcessingEnvironment.java:1267)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing (JavacProcessingEnvironment.java:1382)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations (JavaCompiler.java:1234)
    at com.sun.tools.javac.main.JavaCompiler.compile (JavaCompiler.java:916)
    at com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0 (JavacTaskImpl.java:104)
    at com.sun.tools.javac.api.JavacTaskImpl.invocationHelper (JavacTaskImpl.java:152)
    at com.sun.tools.javac.api.JavacTaskImpl.doCall (JavacTaskImpl.java:100)
    at com.sun.tools.javac.api.JavacTaskImpl.call (JavacTaskImpl.java:94)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess (JavaxToolsCompiler.java:126)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:174)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1134)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:187)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)

I have checked the dependency with cmd: mvn dependency:tree, and found the Lombok version is up to 1.18.28, and also mapstruct:jar:1.5.2.Final

Please help me if you know how this happens.


Solution

  • NB: I'm a lombok contributor.

    This error is what you get when you run lombok 1.18.28 on JDK21. Not JDK17. Check your JDK version (and double-check your lombok version, as you'd get that error very old lombok versions on JDK17). JDK21 support is on its way (and some steps to permanently solve the problem is going on in branch maven-plugin which hasn't been released in a downloadable version just yet.

    Check java version

    You can check your java version with java --version - if it's JDK21, downgrade your JDK, or use the --add-opens strategy

    Check lombok version

    Find the lombok being used in your compilation process. java -jar lombok.jar version will tell you what version it is. If it's less than 1.18.28, upgade it.

    Use --add-opens

    Whereever javac is being invoked, add the following switches:

    javac \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
      -J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
      -cp classpath arguments *.java
    

    That last line are your usual arguments. If you use a build system it can be quite a bit of effort to figure out how to do that, which is why lombok normally takes care of this for you. However, due to either the JDK you use being too recent or the lombok version you have being too outdated, that part isn't working. These --J-add-opens lines do it manually.