问题
From what I understand Kotlin should be able to run using JRE 6. But this code with a foreach on a map fails because of a reference to a Java 8 class (java/util/function/BiConsumer)
CompilerTest.kt:
fun main(args: Array<String>) {
val aMap = mapOf("bar" to "bat")
aMap.forEach { k, v -> println("$k -> $v")}
}
Compile the Kotlin code:
» kotlinc CompilerTest.kt -jvm-target 1.6 -include-runtime -d compilerTest.jar
Testing the compiled code on JRE 6:
» docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:6-jdk-slim java -jar compilerTest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/BiConsumer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at CompilerTestKt.main(CompilerTest.kt:5)
Caused by: java.lang.ClassNotFoundException: java.util.function.BiConsumer
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
... 12 more
Version Information:
» kotlinc -version
info: kotlinc-jvm 1.1.4-3 (JRE 1.8.0_131-b11)
回答1:
Turns out that kotlin Map inherits from java.util.Map. When you call aMap.forEach { k, v -> println("$k -> $v")}, you are calling the Java version of the method.
However, if you change the code to take an entry: aMap.forEach { entry -> println("$entry.key -> $entry.value")}, you are calling the Kotlin version and the code will run on JRE6.
You can flag the dependency on the JRE by compiling with the -no-jdk flag to the compiler.
» kotlinc CompilerTest.kt -no-jdk -jvm-target 1.6 -include-runtime -d compilerTest.jar
CompilerTest.kt:5:10: error: type inference failed: inline fun <K, V> Map<out K, V>.forEach(action: (Map.Entry<K, V>) -> Unit): Unit
cannot be applied to
receiver: Map<String, String> arguments: ((Map.Entry<String, String>, ???) -> Unit)
aMap.forEach { k, v -> println("$k -> $v")}
^
...
回答2:
Try to disable the Instant run, under
Preferences > Build, Execution, Deployment
It was the only thing that worked for me!
回答3:
kotlinc-jvm 1.1.4-3 (JRE 1.8.0_131-b11)
It seems like you are using JDK 8 version. If you are using gradle, you should check your build.gradle.
compile "org.jetbrains.kotlin:kotlin-stdlib" // JDK 6
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7" // JDK 7
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8" // JDK 8
来源:https://stackoverflow.com/questions/46396751/why-does-kotlin-byte-code-reference-java-util-function-biconsumer