I have simple Kotlin code in an existing Java project
class A(val p: Int)
fun main() {
println("Hello World")
println(A::javaClass)
println(A::p)
}
However, this throws an exception
Exception in thread "main" java.lang.NoSuchMethodError: 'void kotlin.jvm.internal.PropertyReference1Impl.<init>(java.lang.Class, java.lang.String, java.lang.String, int)'
at mloop.kt.graphql.TestKt$main$1.<init>(Test.kt)
at mloop.kt.graphql.TestKt$main$1.<clinit>(Test.kt)
at mloop.kt.graphql.TestKt.main(Test.kt:10)
at mloop.kt.graphql.TestKt.main(Test.kt)
build.gradle.kts is also simple
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.20"
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.20")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}
Verified that kotlin-reflect is also listed in runtimeClassPath. However, the same code works in a Kotlin-only project.
compileClasspath - Compile classpath for compilation 'main' (target (jvm)).
+--- org.slf4j:slf4j-api -> 2.0.3
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20
+--- org.jetbrains.kotlin:kotlin-reflect:1.7.20
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
\--- org.projectlombok:lombok:1.18.24
runtimeClasspath - Runtime classpath of compilation 'main' (target (jvm)).
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20
| | +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20
+--- org.jetbrains.kotlin:kotlin-reflect:1.7.20
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
+--- org.jetbrains.kotlin:kotlin-reflect:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib-common:{strictly 1.7.20} -> 1.7.20 (c)
This sounds more like a mismatch in the kotlin stdlib during runtime and a compiled class, not something related to reflection (but I'm not very familiar with reflection so I could be wrong).
It seems like the method with signature
Was added in kotlin 1.4, also see this for another example of the same error.
Not really sure where you kotlin
< 1.4stuff is coming from though, are you perhaps using an old gradle version (although I'm not even sure if that would matter)?Please also add the full stacktrace to the question, instead of just 1 line, that should show what exactly is attempting to call the missing method.