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

查看:82
本文介绍了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)

更新:
我把投掷部分分成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?

更新 2:
我用 Throwable 添加了第二个 catch 块,然后捕获了以下 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

更新 3:
更改标题以更正错误并修复将所有项目文件添加到 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.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

更新:根据下面这个答案.

Update: Changed configurations.compile.collect to configurations.compileClasspath.collect according to this answer below.

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

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