Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics [英] Gradle Project: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

查看:962
本文介绍了Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java项目,在这个项目中,我第一次尝试使用Kotlin。我开始使用Intellij Idea中提供的JavaToKoltin转换器将一些类转换为Kotlin。其中我的自定义异常现在转换为Kotlin。但是有了这个,异常处理就不再正常了。

如果我在java代码中抛出一个自定义异常(例如 MyCustomKotlinException.kt ) ,没有捕获异常(参见下面的代码)。

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

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

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

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

// MyCustomKotlinException.kt
package foo

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

更新:

我将throw部分分成2行(实例创建和抛出),发现问题不在于抛出。在创建实例后留下try块。我的实例创建此Kotlin类有什么问题吗?

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?

Update2:

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

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

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

Update3:

将标题更改为正确的错误并解决了将所有项目文件添加到jar中的问题(请参阅下面的答案) )。
将Kotlin运行时库添加到gradle对我来说不起作用。

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.

推荐答案

将所有项目文件添加到已修复的jar中对我来说问题。我将以下行添加到我的 build.gradle

Adding all project files to the jar fixed the problem for me. I added the following line to my 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.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

这篇关于Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆