1

Ken Kousen的 Kotlin Cookbook 尝试——1.5 执行 Kotlin 脚本会产生“无法实例化类”错误。

$ cat southpole.kts                                                                            
import java.time.*                                                                                                                   
val instant = Instant.now()
val southPole = instant.atZone(ZoneId.of("Antarctica/South_Pole"))
val dst = southPole.zone.rules.isDaylightSavings(instant)
println("It is ${southPole.toLocalTime()} (UTC${southPole.offset}) at the South Pole")
println("The South Pole ${if (dst) "is" else "is not"} on Daylight Savings Time")
$ kotlinc -script southpole.kts
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
error: unable to instantiate class Southpole (southpole.kts): java.lang.NoClassDefFoundError: kotlin/script/templates/standard/ScriptTemplateWithArgs

kotlin version 1.3.50

4

1 回答 1

3

为了工作,println(...)需要一个必须手动添加的 kotlin 运行时。

此处描述了此问题https://discuss.kotlinlang.org/t/possible-kts-bug/10162

...,它(脚本)从模块中获取依赖项,因此您需要明确地将 kotlin-script-runtime 包含到模块依赖项中。(...) 不幸的是,这不是很明显。我们正在考虑可能的解决方案。

这似乎在即将发布的Kotlin 1.3.60版本 中得到了改进https://youtrack.jetbrains.com/issue/KT-33529

作为一种解决方法,请使用:

$ sdk use kotlin 1.3.41
于 2019-10-16T09:13:57.853 回答