2

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
Jan Válek
  • 424
  • 4
  • 14
  • 1
    kotlin-reflect is not on your classpath.  If you're going to run it that way, using explicit libraries and loose .class files, then you'll need to add it to your classpath.  But it's more common to build to a .jar file and run from that; if you build a ‘fat jar’ or .war that includes its dependencies, then you won't need to specify any of them manually. – gidds Mar 28 '21 at 11:18
  • In was thinking it is suppose of Gradle and dependency part for this. Any way your are right. I downloaded jar, added to classpath and its working now. Even if I delete the jar file and declaration stay in "build.gradle". Maybe it was only unsynced "build.gradle" file. I must say this is weird stuff for c# programmer :D – Jan Válek Mar 28 '21 at 16:51

2 Answers2

4

The problem is a missing dependency in build.gradle.

Just add the following to fix.

dependencies {
    ...
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    ...
}
pringi
  • 3,987
  • 5
  • 35
  • 45
0

Somehow it started working. What I did was:

  • download kotlin-reflect.jar from Maven repository
  • next in IntelliJ file -> project structure -> modules -> dependencies add path to directory with kotlin-reflect.jar - confirm OK
  • now it was working.
  • remove this settings, remove downloaded file.
  • edit "build.gradle" insert dependency or make same change, and click sync.

Project is working and I can see this reflect library in external libraries as dependency from Gradle.

As I mentioned in question I'm starting with "Kotlin-jvm" so I suppose this is suboptimal solution but works for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jan Válek
  • 424
  • 4
  • 14