安装 Kotlin-Jupyter: e: java.lang.NoClassDefFoundError: 无法初始化 class org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel

2022-09-02 11:39:04

我是 gradle 的新手,但最近我想在一个项目中使用 Kotlin-Jupyter 笔记本。

我按照说明进行操作,我在 https://github.com/Kotlin/kotlin-jupyter 克隆了存储库...

在我的整个挣扎过程中,我经历了惊人的5-6个错误,所有这些错误都需要几天的时间才能修复。这简直是一场噩梦。

我在键入命令时收到此新错误,这是一个内部异常:gradlew install


> 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

是的,它很长。我不确定错误的原因是什么 - 坦率地说,当涉及到Gradle时,我有点菜鸟。我想知道为什么我会收到这个错误,以及如何解决这个问题,以便我可以继续我的项目。

同样,就在最近,我安装了JDK并为JDK设置了环境变量。我不确定错误是否因此而上升,如果是这样 - 这是我的环境变量,也许我设置错了:

C:\Program Files\Java\jdk-16

此外,这是 gradle 属性文件:

# 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

还有 build.gradle 文件:

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
}

谢谢

汤姆·乔尼


答案 1

我在Android Studio上遇到了完全相同的问题,并通过将Kotlin Gradle插件从1.4.31版本更新到1.5.30版本来解决它。

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

在我的项目级 build.gradle 文件中。


答案 2

不确定是否是相同的问题,但对我来说,添加以下行来解决它gradle.properties

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

推荐