據我瞭解科特林應該能夠使用JRE 6運行,但這段代碼有foreach
地圖上的失敗,因爲對一個Java 8類(java/util/function/BiConsumer
)爲什麼Kotlin字節碼引用java.util.function.BiConsumer?
CompilerTest.kt參考:
fun main(args: Array<String>) {
val aMap = mapOf("bar" to "bat")
aMap.forEach { k, v -> println("$k -> $v")}
}
編譯科特林代碼:
» kotlinc CompilerTest.kt -jvm-target 1.6 -include-runtime -d compilerTest.jar
測試上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
版本信息:
» kotlinc -version
info: kotlinc-jvm 1.1.4-3 (JRE 1.8.0_131-b11)
替代地使用'aMap.forEach {(K,V) - >的println( 「$的k - > $ V」)}' - 注意到'(K,V)'。它在原地進行'Entry'分解 – voddan