76

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.

7
  • Please provide a complete minimal reproducible example, for example with that kotlin source code.
    – GhostCat
    May 26, 2017 at 8:59
  • @GhostCat is this enough for you?
    – FreshD
    May 26, 2017 at 9:13
  • It needs to be enough so that people can repro the issue. Looks like now.
    – GhostCat
    May 26, 2017 at 9:14
  • @FreshD I can't reproduce the issue with the code you provided. May 26, 2017 at 11:50
  • @Christian Yeah you are right. I just tried with this example and it works. But the exception class looks same, but with a different name and also the instance creation in the try catch is same, so no idea why it works just for the example.
    – FreshD
    May 26, 2017 at 12:01

9 Answers 9

53

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.

4
  • Use configurations.compileClasspath now instead of configurations.compile as @simon-o mentioned above.
    – Chris
    Feb 16, 2020 at 19:27
  • Could you, please, give me a step by step of how you generated your project's JAR? I'm having big problems with that Sep 12, 2020 at 1:14
  • 2
    how do I do this on build.gradle.kts?
    – mending3
    Feb 15, 2021 at 6:45
  • @mending3 What happens if you add the above lines into build.gradle.kts?
    – FreshD
    Feb 15, 2021 at 11:23
31

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

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

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) }
    })
}
1
  • 2
    Thanks, when I encountered the exception of 【is a duplicate but no duplicate handling strategy has been set】, I added 【duplicatesStrategy = DuplicatesStrategy.EXCLUDE】 and it was fine
    – Lancer.Yan
    Jun 15, 2022 at 4:43
26

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.

0
15

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

3
  • 2
    I tried to add the runtime lib to my build.gradle (compile "org.jetbrains.kotlin:kotlin-runtime:$kotlin_version"), but does not work for me.
    – FreshD
    May 26, 2017 at 13:09
  • 1
    Perfect! it works for me. Add "kotlin-runtime-1.2.71.jar" then rebuild. That error gone. Kotlin works like a charm! Thank you!
    – Leo Nguyen
    Oct 16, 2018 at 3:36
  • 1
    Worked for me too, though kotlin-runtime has been deprecated for kotlin-stdlib. Nov 29, 2018 at 0:03
6

Thanks for the comment. Indeed compile is deprecated. However the accepted answer does not work with implementation. So I looked up the java library plugin configuration and implementation depends on 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.

Using compile instead of implementation in the dependencies of the build.gradle file solved it for me.

3
  • implementation is newer than compile. Should not be used in replacement Oct 30, 2019 at 11:48
  • @Kishita Thanks for your comment. You are absolutely right and compile should not be used anymore. I was looking for a better alternative and updated my answer.
    – Simon O
    Oct 31, 2019 at 15:35
  • Oh and by the way the reason why you can't use configurations.implementation.collect or configurations.api.collect is that they can not be resolved.
    – Simon O
    Oct 31, 2019 at 15:37
2

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

1

Adding the following solved the issue for me:

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

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

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

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

0

In my case adding maven Bom file worked. It does job of downloading and keeping all the kotlin dependencies in sync.

imports {
        mavenBom 'org.jetbrains.kotlin:kotlin-bom:<<KOTLIN_VERSION>>'
    }

Refer the official documentation here : https://kotlinlang.org/docs/gradle-configure-project.html#other-ways-to-align-versions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.