我正在学习 Kotlin,但这段代码有问题:
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)
我的 build.gradle 看起来像:
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'
}
我试图用谷歌搜索它,但我只找到了对我不起作用的具有“实现”依赖性的解决方案。我怎样才能让这个程序运行?
现在,如果我在 IntelliJ 中编译此代码,它会在崩溃时运行:
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