kotlinintellij-ideakotlin-reflect

Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath


I'm learning Kotlin and I have problem with this code:

import kotlin.reflect.*

fun main(args: Array<String>) {
    val lst = listOf(1,2,3,45,5,6,7,8)
    val mapped = lst.map {Data(::isEven,it)}
    for(item in mapped){
        var result = item.func.call(item.param)
        println("$result ${item.func} ${item.param}")
    }

}

fun isEven(number: Int):Boolean{
    return number%2==0;
}

class Data(val func: KFunction<Any>,val param:Any)

My build.gradle looks like:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.4.31'
    id 'application'
}

group = 'me.janva'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.4.31'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}

test {
    useJUnit()
}

compileKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

compileTestKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

application {
    mainClassName = 'MainKt'
}

I tried to google it, but I only found sollutions with "implementation" dependency which not work for me. How can I make this program run?

Now if I compile this code in IntelliJ it run at crash with this:

C:\Users\janva\OpenJdks\jdk-15.0.2\bin\java.exe -javaagent:C:\Users\janva\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\203.7717.56\lib\idea_rt.jar=52396:C:\Users\janva\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\203.7717.56\bin -Dfile.encoding=UTF-8 -classpath E:\projects\ReflectionTest\build\classes\kotlin\main;C:\Users\janva\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk8\1.4.31\e613be5465ef1e6fd0468707690b7ebf625ea2fe\kotlin-stdlib-jdk8-1.4.31.jar;C:\Users\janva\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.4.31\84ce8e85f6e84270b2b501d44e9f0ba6ff64fa71\kotlin-stdlib-jdk7-1.4.31.jar;C:\Users\janva\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.4.31\a58e0fb9812a6a93ca24b5da75e4b5a0cb89c957\kotlin-stdlib-1.4.31.jar;C:\Users\janva\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common\1.4.31\6dd50665802f54ba9bc3f70ecb20227d1bc81323\kotlin-stdlib-common-1.4.31.jar;C:\Users\janva\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar MainKt
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.CallableReference.getReflected(CallableReference.java:98)
    at kotlin.jvm.internal.FunctionReference.getReflected(FunctionReference.java:63)
    at kotlin.jvm.internal.FunctionReference.getReflected(FunctionReference.java:12)
    at kotlin.jvm.internal.CallableReference.call(CallableReference.java:161)
    at MainKt.main(main.kt:8)

Process finished with exit code 1

Solution

  • The problem is a missing dependency in build.gradle.

    Just add the following to fix.

    dependencies {
        ...
        implementation "org.jetbrains.kotlin:kotlin-reflect"
        ...
    }