How can I use scripts in Kotlin 1.1 M02-EAP?


#1

This blog post mentions support for using Kotlin as a scripting language with JSR-223 (the javax.script API).

I am very eager to try out this feature, but it doesn’t seem like Kotlin has a binding for kotlin, i.e. this test fails for me: https://github.com/JetBrains/kotlin/blob/d335aea6821542d30840299b1a9c07468d081b62/idea/tests/org/jetbrains/kotlin/idea/repl/IdeaJsr223Test.kt

The reason it fails is because engine is null. Is there something I’m missing that I need to do to get it to work? :thinking:


#2

This is specific test for JSR223 host in IntelliJ IDEA. You need to have an infrastructure to make it run. Generic JSR 223 examples are located here: https://github.com/JetBrains/kotlin/tree/master/libraries/examples/ (kotlin-jsr223-*) and the “kotlin-jsr223-local-example” is easiest to follow and reuse.


#3

Doubt the OP cares any more but for future searchers, the key steps to get it to work are:

You need these libraries in your class path (in additon to stdlib):

  • kotlin-compiler
  • kotlin-script-util

In addition, you need a META-INF/services/javax.script.ScriptEngineFactory file in the classpath jar to tell the JSR223 script engine to use the Kotlin script engine. Just adding the above libraries does not do it (probably a security concern). The easiest way to just add the file to your own jar like from this example: