Gradle Project: java.lang.NoClassDefFoundError: ko

2019-03-18 02:55发布

问题:

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

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)

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

回答1:

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


回答2:

You need to configure your project with kotlin. So in 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

Done.



回答3:

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

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 .jar file and running it with the runtime library



回答4:

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



回答5:

it will resolve "Intrinsics" in Kotlin

add these libraries in gradle file

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.41'
    implementation 'nl.dionsegijn:konfetti:1.1.2'


回答6:

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