Executing kotlin script from terminal

On Ubuntu 19.06, I installed Kotlin (1.3.50) via the Snap, then try to execute a kts file from terminal:

test.kts
println("Hello from KTS!")

by running:
kotlinc -script test.kts

But it errors out with:
error: unable to instantiate class Test (test.kts): java.lang.NoClassDefFoundError: kotlin/script/templates/standard/ScriptTemplateWithArgs

Any ideas what I’m doing wrong?

I don’t think you’re doing anything wrong at all — I think it’s a new bug in the Kotlin platform!

I hit the same error yesterday, soon after upgrading to 1.3.50, on a script that’s run fine many times before.

(There have been similar reports before on YouTrack. I couldn’t spot a new one, but I did add a comment to an old one that seems to apply anew.)

Normally you can fix a NoClassDefFoundError by playing with the classpath; but even though I found two different jars in the Kotlin installation that seemed to define the missing class, I couldn’t fix the problem with any amount of playing with -cp, -kotlin-home, $CLASSPATH, or $KOTLIN_HOME.

(If it matters, this is using Kotlin installed by Homebrew; so not related to any IDE.)

@gidds do you have a link to any of those youtrack reports?

The issue is KT-33529. Apparently the fix is confirmed, and will be in Kotlin 1.3.60.

Meanwhile, the recommended workaround is to use the Kotlin compiler via java executable, specifying the kotlin-compiler.jar:
java -jar <path/to/kotlin-compiler.jar> -script test.kts