Kapt is not working properly with OpenJDK 16
Asked Answered
A

5

29

I have an application that uses Kotlin 1.5.0 and Kapt for mapstructs. I have updated my JDK to 16 and I get the following compilation error:

Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.0:kapt (kapt) on project X: Compilation failure
java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x248e24b) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x248e24b
    at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:29)
    at org.jetbrains.kotlin.kapt3.KaptContextForStubGeneration.<init>(KaptContextForStubGeneration.kt:40)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.contextForStubGeneration(Kapt3Extension.kt:285)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:169)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:100)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$invokeExtensionsOnAnalysisComplete(TopDownAnalyzerFacadeForJVM.kt:111)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:121)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:85)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:514)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:505)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:112)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:505)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:189)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:155)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:88)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
    at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execCompiler(KotlinCompileMojoBase.java:228)
    at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execCompiler(K2JVMCompileMojo.java:237)
    at org.jetbrains.kotlin.maven.kapt.KaptJVMCompilerMojo.execCompiler(KaptJVMCompilerMojo.java:152)
    at org.jetbrains.kotlin.maven.kapt.KaptJVMCompilerMojo.execCompiler(KaptJVMCompilerMojo.java:44)
    at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execute(KotlinCompileMojoBase.java:209)
    at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execute(K2JVMCompileMojo.java:222)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)

I have seen that there is an open issue with Kapt and JDK 16: https://youtrack.jetbrains.com/issue/KT-45545, I'm trying to use --add-opens as args options in the kotlin-maven-plugin but seems they are not working properly:

<plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>kapt</id>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/main/kotlin</sourceDir>
                            <sourceDir>src/main/java</sourceDir>
                        </sourceDirs>
                        <annotationProcessorPaths>
                            <annotationProcessorPath>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${mapstruct.version}</version>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <args>
                    <arg>-Xjsr305=strict</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
                    <arg>-X--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED</arg>
                </args>
                <compilerPlugins>
                    <plugin>spring</plugin>
                </compilerPlugins>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
</plugin>

when I execute again maven with this args options tells me the following:

[INFO] --- kotlin-maven-plugin:1.5.0:kapt (kapt) @ X ---
[WARNING] Source root doesn't exist: /home/nico/IdeaProjects/X/X/src/main/java
[WARNING] 'tools.jar' was not found, kapt may work unreliably
[INFO] Applied plugin: 'spring'
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
[WARNING] Flag is not supported by this version of the compiler: -X--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
  • How can I add these options to kotlin-maven-plugin?
  • Is there any other posibility to run Kapt with JDK 16 and maven?
Anthea answered 12/5, 2021 at 18:21 Comment(0)
M
13

Update 2022

Kapt should work correctly out-of-the-box with JDK 16+ without any workarounds if you use Kotlin 1.6.0+

This commit introduced the necessary changes in Kotlin.

Original answer

How can I add these options to kotlin-maven-plugin?

Unfortunely, you can't. kotlin-maven-plugin does not accept JVM arguments.

Is there any other posibility to run Kapt with JDK 16 and maven?

What helped me was creating a .mvn/jvm.config file in my project with the following content:

 --illegal-access=permit --add-opens jdk.compiler/com.sun.tools.javac.util=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.jvm=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED

It works correct when I build the project in command line but keep in mind it might not work when you use IDE tools to manage Maven build, for example IntelliJ completely ignores the file.

Marrakech answered 4/6, 2021 at 14:43 Comment(2)
I am using JDK 11, and had the same error after migrating to gradle KTS from groovy. Not sure how this relates to my migration though! Even downgrading to JDK8 didn't solve the issue! Adding the above line fixed the issue.Nafis
Or in your build.gradle.kts file, add: tasks.withType<org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask>() .configureEach { listOf("util", "file", "main", "jvm", "processing", "comp", "tree", "api", "parser", "code") .flatMap { listOf("--add-opens", "jdk.compiler/com.sun.tools.javac.$it=ALL-UNNAMED") } .forEach(kaptProcessJvmArgs::add) }Ashcraft
D
40

According to JetBrains comment:

Add org.gradle.jvmargs=--illegal-access=permit to gradle.properties

Options can be combined by separating them with a space:

org.gradle.jvmargs=-Xmx1536m --illegal-access=permit
Decurrent answered 29/7, 2021 at 7:55 Comment(2)
My gradle.properties already contains org.gradle.jvmargs=-Xmx1536m. How can I combine the two options?Hildick
@Hildick you can combine options separated by spaces: org.gradle.jvmargs=-Xmx1536m --illegal-access=permitDecurrent
M
13

Update 2022

Kapt should work correctly out-of-the-box with JDK 16+ without any workarounds if you use Kotlin 1.6.0+

This commit introduced the necessary changes in Kotlin.

Original answer

How can I add these options to kotlin-maven-plugin?

Unfortunely, you can't. kotlin-maven-plugin does not accept JVM arguments.

Is there any other posibility to run Kapt with JDK 16 and maven?

What helped me was creating a .mvn/jvm.config file in my project with the following content:

 --illegal-access=permit --add-opens jdk.compiler/com.sun.tools.javac.util=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.jvm=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED

It works correct when I build the project in command line but keep in mind it might not work when you use IDE tools to manage Maven build, for example IntelliJ completely ignores the file.

Marrakech answered 4/6, 2021 at 14:43 Comment(2)
I am using JDK 11, and had the same error after migrating to gradle KTS from groovy. Not sure how this relates to my migration though! Even downgrading to JDK8 didn't solve the issue! Adding the above line fixed the issue.Nafis
Or in your build.gradle.kts file, add: tasks.withType<org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask>() .configureEach { listOf("util", "file", "main", "jvm", "processing", "comp", "tree", "api", "parser", "code") .flatMap { listOf("--add-opens", "jdk.compiler/com.sun.tools.javac.$it=ALL-UNNAMED") } .forEach(kaptProcessJvmArgs::add) }Ashcraft
C
1

Now I still not found solution for JDK 16

But I can work arround by using JDK 15, you can download at: https://www.oracle.com/java/technologies/javase/jdk15-archive-downloads.html

Crabbing answered 2/6, 2021 at 9:2 Comment(2)
I would rather to use JDK 16 because I use it in my server, so downgrade to JDK 15 is not an option.Anthea
Unfortunately, this is the only long-term solution, as on JDK17 kapt cannot be run by any means. However, it's only the BUILD that needs to be done on JDK15 (optimally) or older. Build results can then be deployed to run on the JDK17 and newer.Rachmaninoff
P
0

I using Mac Intel, kotlin version 1.5, AndroidStudio latest stable version and I face same build error, I already tried to add

  • --illegal-access=permit
  • Force org.xerial:sqlite-jdbc:3.34.0 version
  • Restart AndroidStudio, restart computer

But none of them work, there is a answer which said that kotlin 1.6 fixed the problem but I can not upgrade

Then I try download a new Android Studio version (beta version), and it work, no more build error

Punt answered 27/7, 2022 at 1:24 Comment(0)
L
0

Worked when Kotlin Gradle Plugin Version was upgraded to 1.6.0+

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'

Kotlin plugins for Gradle

Lingo answered 14/4, 2023 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.