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

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: kotlin/IdeaJsr223Test.kt at d335aea6821542d30840299b1a9c07468d081b62 · JetBrains/kotlin · GitHub

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:

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: kotlin/libraries/examples at master · JetBrains/kotlin · GitHub (kotlin-jsr223-*) and the “kotlin-jsr223-local-example” is easiest to follow and reuse.

1 Like

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:

2 Likes