1

Attempt at Kotlin Cookbook by Ken Kousen -- 1.5 Executing a Kotlin Script produces 'unable to instantiate class' error.

$ 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

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
  • please specify java and kotlinc version – Naor Tedgi Oct 16 '19 at 08:57
  • by the way the script works for : openjdk version "1.8.0_212" OpenJDK Runtime Environment (Zulu 8.38.0.13-CA-linux64) (build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (Zulu 8.38.0.13-CA-linux64) (build 25.212-b04, mixed mode) , kotlinc-jvm 1.3.41 (JRE 1.8.0_212-b04) – Naor Tedgi Oct 16 '19 at 08:57

1 Answers1

3

In order to work, println(...) needs a kotlin runtime which has to be added manually.

This issue is described here 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.

This seems to be improved in the upcoming Kotlin 1.3.60 release https://youtrack.jetbrains.com/issue/KT-33529

As as a work-around, use:

$ sdk use kotlin 1.3.41
Sergei Voitovich
  • 2,804
  • 3
  • 25
  • 33