I'm trying to evaluate Kotlin code inside JVM using the Java Scripting API.
try {
ScriptEngineManager().getEngineByExtension("kts").let {
it.eval("val f: (CommandContext.()->Any?) = {\n${this.args.joinToString(" ")}\n}; return f") as (CommandContext.()->Any?)
}().let { embed.setDescription(it.toString()) }
} catch (ex: Exception) {
embed.setColor(Color.RED)
embed.setDescription(StringWriter().also { ex.printStackTrace(PrintWriter(it)) }.toString())
}
But... ScriptEngineManager().getEngineByExtension("kts")
is returning me a null value. I already added the META-INF/services
file:
File Name: javax.script.ScriptEngineFactory
File Content: org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
It sould work according to JetBrains: https://github.com/JetBrains/kotlin/tree/master/libraries/examples/kotlin-jsr223-local-example
I just fixed by adding
compile 'org.jetbrains.kotlin:kotlin-compiler:1.3.11'
compile 'org.jetbrains.kotlin:kotlin-script-runtime:1.3.11'
compile 'org.jetbrains.kotlin:kotlin-script-util:1.3.11'
compile 'org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11'
to my build.gradle
.
PS: 1.3.11 is my Kotlin version.