kotlin - Kotlin脚本无法实例化类

标签 kotlin kotlin-script

尝试Kotlin Cookbook by Ken Kousen -- 1.5 Executing a Kotlin Script会产生“无法实例化类”错误。

$ 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

最佳答案

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

这个问题在这里描述https://discuss.kotlinlang.org/t/possible-kts-bug/10162

..., it (script) takes dependencies from the module, so you need to include kotlin-script-runtime to the module dependencies explicitly. (...) Unfortunately, it is not very obvious. We’re thinking about possible solutions.



即将发布的 Kotlin 1.3.60 版本似乎对此有所改善
https://youtrack.jetbrains.com/issue/KT-33529

作为解决方法,请使用:
$ sdk use kotlin 1.3.41

关于kotlin - Kotlin脚本无法实例化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405880/

相关文章:

java - 评估具有依赖项的 kotlin 脚本文件

Android:Kotlin:自定义 webView - 无法作为函数调用。找不到函数 'invoke()'

intellij-idea - 使用 IntelliJ IDEA 构建 Gradle 多模块的正确结构

Kotlin 脚本找不到 Ktor CIO 类

android - 如何在 Android 项目中使用 Kotlin 工作表

kotlin - 每当我尝试运行任何脚本时,Kotlin脚本支持均会因 “wrong number of arguments”而失败

android - Mockito:在 UI 测试中模拟最终类

java - 项目未在项目中使用 Kotlin 进行编译

android - 如何使用Kotlin从for循环中打印特定数字