Gradle项目:java.lang.NoClassDefFoundError:Kotlin/jvm/内部/内部函数

epfja78i  于 2022-11-16  发布在  Kotlin
关注(0)|答案(8)|浏览(97)

我正在做一个Java项目,在这个项目中,我第一次尝试使用Kotlin。我开始用Intellij Idea中提供的JavaToKoltin转换器将一些类转换为Kotlin。其中,我的自定义异常现在被转换为Kotlin。但是这样一来,异常处理就不能正常工作了。
如果我在java代码中抛出一个自定义异常(例如MyCustomKotlinException.kt),则不会捕获该异常(请参见下面的代码)。

// Example.java
package foo    

import java.util.*;
import java.lang.*;
import java.io.*;
import foo.MyCustomKotlinException;

class Example
{
    public static void main (String[] args)
    {
        try {
            // Do some stuff
            // if Error
            MyCustomKotlinException e = new MyCustomKotlinException("Error Message");
            throw e;
        } catch (MyCustomKotlinException e) {  // <-- THIS PART IS NEVER REACHED
            // Handle Exception
        } catch (Throwable e) {
            e.printStackTrace(); <-- This is catched
        } finally {
            // Finally ...
        }
    }
}

那么,有人能向我解释一下为什么这个异常不是catch吗?MyCustomKotlinException是从Kotlins RuntimeException继承的,它只是java.lang.RuntimeException的一个别名。

// MyCustomKotlinException.kt
package foo

class MyCustomKotlinException(err: String) : RuntimeException(err)

更新日期:

我把throw部分分成了2行(示例创建和throw),发现问题不在于throw。try块在示例创建之后留下了。我这个Kotlin类的示例创建有什么问题吗?

更新2:

我用Throwable添加了第二个catch块,并捕获了下面的Throwable。

java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
...
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics

更新3:

修改了标题以更正错误,并修复了将所有项目文件添加到jar的问题(请参见下面的答案)。

zfciruhq

zfciruhq1#

将所有项目文件添加到jar中为我解决了这个问题。

jar {
    manifest {
        attributes ...
    }
    // This line of code recursively collects and copies all of a project's files
    // and adds them to the JAR itself. One can extend this task, to skip certain
    // files or particular types at will
    from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

更新:根据下面的this答案,将configurations.compile.collect更改为configurations.compileClasspath.collect

noj0wjuj

noj0wjuj2#

您需要使用Kotlin配置您的项目。因此,在Android Studio中:
1.单击工具=〉Kotlin=〉在项目中配置kotlin
1.然后在对话框中检查:包含Kotlin文件的所有模块
1.并选择版本
1.按OK
好了,好了

k97glaaz

k97glaaz3#

此错误可能是由于简单的jar任务没有获取其所有的运行时依赖项。
从gradle文档中,您可以在build.gradle.kts中创建一个“fatJar”任务或将其添加到jar任务中:

tasks.withType<Jar> {
    // Otherwise you'll get a "No main manifest attribute" error
    manifest {
        attributes["Main-Class"] = "com.example.MainKt"
    }

    // To avoid the duplicate handling strategy error
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    // To add all of the dependencies
    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}
aiazj4mn

aiazj4mn4#

我要说的是,您试图在没有kotlin-runtime库的情况下运行Kotlin代码
检查您正在使用的系统并添加必要的jar文件。

plicqrtu

plicqrtu5#

谢谢你的评论。确实compile是不赞成的。但是接受的答案不适用于implementation。所以我查了一下java库插件的配置,implementation依赖于compileClasspath。
现在我的解决方案是

jar {
    manifest {
        attributes ...
    }
    // This line of code recursively collects and copies all of a project's files
    // and adds them to the JAR itself. One can extend this task, to skip certain
    // files or particular types at will
    from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50"
    //...
}

我觉得这应该由org.jetbrains.Kotlin插件来完成。
在build.gradle文件的依赖项中使用compile而不是implementation,我就解决了这个问题。

pcrecxhr

pcrecxhr6#

在控制台中用Ant编译我的项目时也遇到了同样的问题。我已经把kotlin-stdlib.jar插入到类路径中,问题就消失了。

j0pj023g

j0pj023g7#

添加以下内容为我解决了这个问题:

dependencies {
    "kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
}

下面是libs/gradle-plugins/kotlin的内容:

annotations-13.0.jar
commons-codec-1.9.jar
commons-logging-1.2.jar
gradle-download-task-3.4.3.jar
gson-2.8.5.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
kotlin-android-extensions-1.3.40.jar
kotlin-annotation-processing-gradle-1.3.40.jar
kotlin-build-common-1.3.40.jar
kotlin-compiler-1.3.40.jar
kotlin-compiler-embeddable-1.3.40.jar
kotlin-compiler-runner-1.3.40.jar
kotlin-daemon-client-1.3.40.jar
kotlin-gradle-plugin-1.3.40.jar
kotlin-gradle-plugin-api-1.3.40.jar
kotlin-gradle-plugin-model-1.3.40.jar
kotlin-reflect-1.3.40.jar
kotlin-runtime-1.2.71.jar
kotlin-script-runtime-1.3.40.jar
kotlin-scripting-common-1.3.40.jar
kotlin-scripting-compiler-embeddable-1.3.40.jar
kotlin-scripting-compiler-impl-embeddable-1.3.40.ja
kotlin-scripting-jvm-1.3.40.jar
kotlin-stdlib-1.3.40.jar
kotlin-stdlib-common-1.3.40.jar
kotlin-stdlib-jdk7-1.3.40.jar
kotlin-stdlib-jdk8-1.3.40.jar
kotlinx-coroutines-core-1.1.1.jar
org.jetbrains.kotlin.jvm.gradle.plugin-1.3.40.jar
trove4j-1.0.20181211.jar

完整的gradle.build.kts(离线设置):

buildscript {
    dependencies {
        classpath(fileTree("libs/gradle-plugins/kotlin"))
    }
}

plugins {
    java
    `java-library`
}

apply(plugin = "kotlin")

version = "2019.06.1"

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions.jvmTarget = "12"
}

repositories {
    flatDir {
        dirs("libs/compile")
        dirs("libs/provided")
    }
}

dependencies {
    "kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
    compileOnly(":javaee-api-8.0")
    api(":kotlin-stdlib-common-1.3.40")
    api(":kotlin-stdlib-1.3.40")
    api(":kotlin-stdlib-jdk7-1.3.40")
    api(":kotlin-stdlib-jdk8-1.3.40")
    api(":gson-2.8.5")
}
mum43rcc

mum43rcc8#

在我的例子中,在迁移到Kotlin1.3时,settings.gradle中的enableFeaturePreview导致了这个问题。

相关问题