Gradle Ktlint plugin fails on Java 16

Viewed 1390

After upgrading to Java 16 I am not able to make ktlint gradle plugin to work. It's throwing

Execution failed for task ':runKtlintCheckOverMainSourceSet'.
A failure occurred while executing org.jlleitschuh.gradle.ktlint.worker.KtLintWorkAction
   java.lang.ExceptionInInitializerError (no error message)

Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task     ':runKtlintCheckOverMainSourceSet'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$3(ExecuteActionsTaskExecuter.java:186)
    at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:268)

Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jlleitschuh.gradle.ktlint.worker.KtLintWorkAction
    at org.gradle.workers.internal.DefaultWorkerExecutor$WorkItemExecution.waitForCompletion(DefaultWorkerExecutor.java:336)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:142)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:94)
    ...
Caused by: java.lang.ExceptionInInitializerError
        at org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel.<clinit>(LanguageLevel.java:25)
        at org.jetbrains.kotlin.com.intellij.core.CoreLanguageLevelProjectExtension.<init>(CoreLanguageLevelProjectExtension.java:26)
        at org.jetbrains.kotlin.com.intellij.core.JavaCoreProjectEnvironment.<init>(JavaCoreProjectEnvironment.java:42)
        ...
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.util.ResourceBundle.setParent(java.util.ResourceBundle) accessible: module java.base does not "opens java.util" to unnamed module @58f87189
        at org.jetbrains.kotlin.com.intellij.util.ReflectionUtil.makeAccessible(ReflectionUtil.java:252)
        at org.jetbrains.kotlin.com.intellij.util.ReflectionUtil.getDeclaredMethod(ReflectionUtil.java:269)
        at org.jetbrains.kotlin.com.intellij.DynamicBundle.<clinit>(DynamicBundle.java:22)
        ... 32 more

I am aware that it's caused by stricter module checks (JEP 396) but I am not able to set the JVM args for the plugin. I tried:

  • Setting env variable JAVA_OPTS=--illegal-access=warn
  • Setting org.gradle.jvmargs=--illegal-access=warn -Dkotlin.daemon.jvm.options=--illegal-access=warn in gradle.properties

But neither of the attempts helped, I am out of ideas.

2 Answers

It seems that Java 16 is not supported by Gradle 6.x and will be supported by Gradle 7.

You can update the Gradle plugin to get the latest version according to the migration guide:

gradle wrapper --gradle-version 7.0

That should fix this error message. Otherwise, you can also change the version in the file gradle/wrapper/gradle-wrapper.properties.

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip

I also updated the kotlin plugins like kotlin("jvm") to the latest version 1.4.32.

Related