Gradle 项目:java.lang.NoClassDefFoundError:kotlin/jvm/internal/Intrinsics

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44197521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:24:29  来源:igfitidea点击:

Gradle Project: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

javaexceptionexception-handlingkotlin

提问by FreshD

I'm working on a Java project and within this project I did my first try with Kotlin. I started converting some classes to Kotlin with the JavaToKoltin converter provided in the Intellij Idea. Among others my custom exceptions are now converted to Kotlin. But with this the exception handling does not work correct anymore.
If I throw one of my custom exceptions (e.g. MyCustomKotlinException.kt) within the java code, the exception is not catched (see code below).

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

// 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 ...
        }
    }
}

So can anyone explain to me why the exception is not catch. MyCustomKotlinExceptionis inheriting from Kotlins RuntimeException, which is just an alias to java.lang.RuntimeException.

所以任何人都可以向我解释为什么没有捕获异常。MyCustomKotlinException继承自 Kotlins RuntimeException,它只是java.lang.RuntimeException.

// MyCustomKotlinException.kt
package foo

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

Update:
I split the throw part into 2 lines (instance creation and throwing) and found that the problem is not the throwing. The try block is left after the instance creation. Is anything wrong with my instance creation of this Kotlin class?

更新:
我把投掷部分分成2行(实例创建和投掷),发现问题不是投掷。实例创建后会留下 try 块。我创建的这个 Kotlin 类的实例有什么问题吗?

Update2:
I added a second catch block with Throwableand the following Throwable is caught.

Update2:
我添加了第二个 catch 块,Throwable并捕获了以下 Throwable。

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

Update3:
Changed the title to correct error and fixed problem with adding all project files to jar (see answer below). Adding the Kotlin runtime lib to gradle does not work for me.

更新 3:
更改标题以更正错误并修复将所有项目文件添加到 jar 的问题(请参阅下面的答案)。将 Kotlin 运行时库添加到 gradle 对我不起作用。

采纳答案by FreshD

Adding all project files to the jar fixed the problem for me. I added the following line to my build.gradle

将所有项目文件添加到 jar 中为我解决了这个问题。我将以下行添加到我的build.gradle

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) } }
}

Update: Changed configurations.compile.collectto configurations.compileClasspath.collectaccording to thisanswer below.

更新:根据下面的这个答案更改configurations.compile.collect为。configurations.compileClasspath.collect

回答by Pelocho

I'm going to say that you're trying to run Kotlin code without the kotlin-runtimelibrary

我要说的是你试图在没有kotlin-runtime库的情况下运行 Kotlin 代码

Check which system you're using and add the neccesary jar file. You can verify that this is your issue by packaging your project into a .jarfile and running it with the runtime library

检查您使用的系统并添加必要的 jar 文件。您可以通过将您的项目打包成一个.jar文件并使用运行时库运行它来验证这是您的问题

回答by Suraj Vaishnav

You need to configure your project with kotlin. So in Android Studio:

您需要使用 kotlin 配置您的项目。所以在 Android Studio 中:

  1. click on Tools => kotlin => Configure kotlin in project

  2. Then in dialog check : All module containing kotlin files

  3. and select version

  4. press ok

  1. 单击工具 => kotlin => 在项目中配置 kotlin

  2. 然后在对话框中检查:包含 kotlin 文件的所有模块

  3. 并选择版本

  4. 按确定

Done.

完毕。

回答by DmitryKanunnikoff

Had the same problem, compiling my project with Ant in console. I've edded kotlin-stdlib.jar into classpath and problem has gone.

有同样的问题,在控制台中用 Ant 编译我的项目。我已经将 kotlin-stdlib.jar 加入了类路径,问题就解决了。

回答by matfax

In my case, a enableFeaturePreviewin the settings.gradle caused this issue when migrating to Kotlin 1.3.

就我而言,enableFeaturePreview在迁移到 Kotlin 1.3 时,settings.gradle 中的 a 导致了这个问题。

回答by Eng.Fouad

Adding the following solved the issue for me:

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

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

Here is the content of 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


The complete gradle.build.kts(offline setup):

完整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")
}

回答by Simon O

Thanks for the comment. Indeed compileis deprecated. However the accepted answer does not work with implementation. So I looked up the java library plugin configurationand implementationdepends on compileClasspath.

感谢您的评论。确实compile已弃用。但是,接受的答案不适用于implementation. 于是查了一下java库插件配置implementation依赖compileClasspath。

So my solution for now is to add

所以我现在的解决方案是添加

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) } }
}

with

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

I feel like this should be done by the org.jetbrains.kotlin.jvm plugin.

我觉得这应该由 org.jetbrains.kotlin.jvm 插件来完成。

Using compileinstead of implementationin the dependencies of the build.gradle file solved it for me.

在 build.gradle 文件的依赖项中使用compile而不是implementation为我解决了这个问题。

回答by Sylhare

This error is likely due to the fact that the simple jar task doesn't take all its runtime dependencies.

这个错误很可能是由于简单的 jar 任务没有获取其所有运行时依赖项。

From gradle documentation, In your build.gradle.ktsyou can either create a "fatJar" task or add that to your jar task:

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 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) }
    })
}