Installing Kotlin-Jupyter: e: java.lang.NoClassDefFoundError: Could not initialize class org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel

Viewed 25113

I'm new to gradle, but recently I wanted to use Kotlin-Jupyter notebook for a project.

I followed the instructions, I cloned the repo at https://github.com/Kotlin/kotlin-jupyter...

Throughout my struggles, I have gone through a whopping 5-6 errors, all of which have taken days on end to fix. It has quite literally been a nightmare.

I am getting this new error when typing in the command gradlew install, it is an internal exception:


> Task :kotlin-jupyter-publish:compileKotlin FAILED
The `kotlin-dsl` plugin applied to project ':kotlin-jupyter-publish' enables experimental Kotlin compiler features. For more information see https://docs.gradle.org/6.8.2/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin
e: java.lang.NoClassDefFoundError: Could not initialize class org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel
        at org.jetbrains.kotlin.com.intellij.core.CoreLanguageLevelProjectExtension.<init>(CoreLanguageLevelProjectExtension.java:26)
        at org.jetbrains.kotlin.com.intellij.core.JavaCoreProjectEnvironment.<init>(JavaCoreProjectEnvironment.java:42)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreProjectEnvironment.<init>(KotlinCoreProjectEnvironment.kt:26)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$ProjectEnvironment.<init>(KotlinCoreEnvironment.kt:118)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:420)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createCoreEnvironment(K2JVMCompiler.kt:226)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:152)
        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.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:371)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:105)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:249)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:38)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:80)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:92)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:602)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:93)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1644)
        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 java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:691)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:831)


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':kotlin-jupyter-publish:compileKotlin'.
> Internal compiler error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

Yes, it is quite long. I am unsure what the cause of the error is - to be quite frank, I'm a bit of a noob when it comes to Gradle. I was wondering why I am getting this error and how I could fix this so I can proceed with my project.

Again, just recently I installed the JDK and set the environment variable for the JDK. I am not sure if the error has risen because of this, if so - here is my environment variable, maybe I have set it wrong:

C:\Program Files\Java\jdk-16

Also this is the gradle properties file:

# kotlinVersion=1.4.255-SNAPSHOT
kotlinVersion=1.4.31
kotlinLanguageLevel=1.4
jvmTarget=1.8

shadowJarVersion=5.2.0
kotlinxSerializationVersion=1.1.0
ktlintGradleVersion=9.4.1
ktlintVersion=0.40.0
junitVersion=5.6.2
slf4jVersion=1.7.30
khttpVersion=1.0.0
http4kVersion=4.4.0.1
artifactsPath=build/artifacts

baseVersion=0.8.3

projectRepoUrl=https://github.com/Kotlin/kotlin-jupyter
docsRepo=git@github.com:ileasile/kotlin-jupyter-docs.git
pushRepoUrl=git@github.com:Kotlin/kotlin-jupyter.git

jupyterApiVersion=0.8.3.291
kotlin.jupyter.add.api=false
kotlin.jupyter.add.scanner=false

org.gradle.jvmargs=-Xmx512M

Also build.gradle file:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlinx.jupyter.build.getFlag
import org.jetbrains.kotlinx.jupyter.plugin.options
import org.jetbrains.kotlinx.jupyter.publishing.addPublication
import org.jetbrains.kotlinx.jupyter.publishing.applyNexusPlugin
import org.jlleitschuh.gradle.ktlint.KtlintExtension

plugins {
    kotlin("jvm")
    kotlin("jupyter.api") apply false
    kotlin("plugin.serialization")
    id("com.github.johnrengelman.shadow")
    id("org.jlleitschuh.gradle.ktlint")
    id("org.jetbrains.kotlinx.jupyter.dependencies")
    id("org.jetbrains.kotlinx.jupyter.publishing")
    id("org.jetbrains.kotlinx.jupyter.doc")
}

extra["isMainProject"] = true

val kotlinxSerializationVersion: String by project
val ktlintVersion: String by project
val junitVersion: String by project
val slf4jVersion: String by project
val khttpVersion: String by project

val docsRepo: String by project

val taskOptions = project.options()
val deploy: Configuration by configurations.creating

deploy.apply {
    exclude("org.jetbrains.kotlinx", "kotlinx-serialization-json-jvm")
    exclude("org.jetbrains.kotlinx", "kotlinx-serialization-core-jvm")
}

applyNexusPlugin()

fun KtlintExtension.setup() {
    version.set(ktlintVersion)
    enableExperimentalRules.set(true)
}

ktlint {
    setup()
}

subprojects {
    apply(plugin = "org.jlleitschuh.gradle.ktlint")

    ktlint {
        setup()
    }
}

allprojects {
    val kotlinLanguageLevel: String by rootProject
    val jvmTarget: String by rootProject

    tasks.withType(KotlinCompile::class.java).all {
        kotlinOptions {
            languageVersion = kotlinLanguageLevel
            this.jvmTarget = jvmTarget
        }
    }
}

dependencies {
    // Dependency on module with compiler.
    api(project(":shared-compiler"))

    // Standard dependencies
    implementation(kotlin("stdlib"))
    implementation(kotlin("reflect"))
    implementation(kotlin("stdlib-jdk8"))
    implementation("org.jetbrains:annotations:20.1.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")

    // Embedded compiler and scripting dependencies
    implementation(kotlin("compiler-embeddable"))
    implementation(kotlin("scripting-compiler-impl-embeddable"))
    implementation(kotlin("scripting-compiler-embeddable"))
    implementation(kotlin("scripting-ide-services"))
    implementation(kotlin("main-kts"))
    implementation(kotlin("script-util"))
    implementation(kotlin("scripting-common"))

    // Embedded version of serialization plugin for notebook code
    implementation(kotlin("serialization"))

    // Logging
    implementation("org.slf4j:slf4j-api:$slf4jVersion")
    runtimeOnly("org.slf4j:slf4j-simple:$slf4jVersion")

    // ZeroMQ library for implementing messaging protocol
    implementation("org.zeromq:jeromq:0.5.2")

    // Clikt library for parsing output magics
    implementation("com.github.ajalt:clikt:2.8.0")

    // Serialization implementation for kernel code
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")

    // Test dependencies: kotlin-test and Junit 5
    testImplementation(kotlin("test"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
    testImplementation("io.kotlintest:kotlintest-assertions:3.1.6")

    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

    deploy(project(":lib"))
    deploy(project(":api"))
    deploy(kotlin("script-runtime"))
}

tasks.register("publishLocal") {
    group = "publishing"

    dependsOn(
        tasks.condaPackage,
        tasks.pyPiPackage
    )
}

tasks.named("closeRepository") {
    mustRunAfter("publishToSonatype")
}

tasks.register("publishToSonatypeAndRelease") {
    group = "publishing"

    dependsOn("publishToSonatype", "closeAndReleaseRepository")
}

tasks.register("publishToPluginPortal") {
    group = "publishing"

    dependsOn(
        ":kotlin-jupyter-api-gradle-plugin:publishPlugins"
    )
}

tasks.jar {
    manifest {
        attributes["Main-Class"] = taskOptions.mainClassFQN
        attributes["Implementation-Version"] = project.version
    }
}

tasks.shadowJar {
    archiveBaseName.set(taskOptions.packageName)
    archiveClassifier.set("")
    mergeServiceFiles()

    manifest {
        attributes(tasks.jar.get().manifest.attributes)
    }
}

tasks.test {
    val doParallelTesting = getFlag("test.parallel", true)

    /**
     *  Set to true to debug classpath/shadowing issues, see testKlaxonClasspathDoesntLeak test
     */
    val useShadowedJar = getFlag("test.useShadowed", false)

    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }

    if (useShadowedJar) {
        dependsOn(tasks.shadowJar.get())
        classpath = files(tasks.shadowJar.get()) + classpath
    }

    systemProperties = mutableMapOf(
        "junit.jupiter.displayname.generator.default" to "org.junit.jupiter.api.DisplayNameGenerator\$ReplaceUnderscores",

        "junit.jupiter.execution.parallel.enabled" to doParallelTesting.toString() as Any,
        "junit.jupiter.execution.parallel.mode.default" to "concurrent",
        "junit.jupiter.execution.parallel.mode.classes.default" to "concurrent"
    )
}

tasks.processResources {
    dependsOn(tasks.buildProperties)
}

tasks.check {
    dependsOn(tasks.checkReadme)
}

tasks.publishDocs {
    docsRepoUrl.set(docsRepo)
}

addPublication {
    publicationName = "kernel"
    artifactId = "kotlin-jupyter-kernel"
    description = "Kotlin Jupyter kernel published as artifact"
    packageName = artifactId
}

Thanks,

Tom Joney

5 Answers

Not sure if it is the same issue, but for me adding the following line to gradle.properties solved it

org.gradle.jvmargs=-Dkotlin.daemon.jvm.options=--illegal-access=permit

I had the exact same issue on Android Studio and solved it by updating the Kotlin Gradle plugin from version 1.4.31 to version 1.5.30.

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"

In my project-level build.gradle file.

Using a JDK version that isn't 16 (I used JDK 15) should solve the problem. Also, make sure you change all of your environment variables and relaunch whatever command-line you are using.

Had the similar issue when building an intellij plugin through gradle. The solution was to remove all content in cache folder from .gradle in your home dir.

On react native, I had to Add the following to fix it:

classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")

in android/build.gradle under buildscript.dependencies after classpath("com.android.tools.build:gradle:7.1.1")

Related