JSR 223 compliant scripting in Kotlin 1.6.21

I had used successfully the JSR 223 compliant Kotlin Script Engine in Kotlin 1.6.0, but that code fails in kotlin 1.6.21, outputting a message about null CompilerConfiguration.
Does anyone works with JSR 223 scripting in Kotlin 1.6.21?

I am using Kotlin 1.6.21 scripting engine in my Java project. I can try to produce a minimal example, if this is relevant. When everything is configured correctly, org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory produces a working javax.script.ScriptEngine.

1 Like

unfortunately it doesn’t work. I tried:

kotlinEngine = (new kotlin.script.experimental.jsr223.KotlinJsr223DefaultScriptEngineFactory()).getScriptEngine();

and I get this output:

ERROR @NotNull method org/jetbrains/kotlin/config/CompilerConfiguration.ge
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.asJsr223EvalResult(KotlinJsr223JvmScriptEngineBase.kt:104)

at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:63)

at kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl.compileAndEval(KotlinJsr223ScriptEngineImpl.kt:95)

at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31)

at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)

at kotlinLabGlobal.Interpreter.GlobalValues.initKotlinEngine(GlobalValues.java:460)

at kotlinLabExec.kotlinLab.kotlinLab.<init>(kotlinLab.java:123)

at kotlinLabExec.kotlinLab.kotlinLab.main(kotlinLab.java:240)

This currently works for me as expected:

import kotlin.script.experimental.jsr223.KotlinJsr223DefaultScriptEngineFactory;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class TestApp {
    public static void main(String[] args) {
        final ScriptEngine kotlinEngine = new KotlinJsr223DefaultScriptEngineFactory().getScriptEngine();

        final String script = "2 + 2";

        try {
            System.out.println(kotlinEngine.eval(script));
        } catch (ScriptException e) {
            throw new RuntimeException("Can't evaluate " + script);
        }
    }
}

I am using kotlin-scripting-jsr223 v. 1.6.21.

I remember having some issues before, also with a NPE, and it was probably due to missing runtime dependencies. I believe kotlin-scripting-jsr223 has them all.

Unfortunately at me it still fails with the message:
DEBUG Using JVM IR backend
WARNING Lombok Kotlin compiler plugin is an experimental feature. See: Stability of Kotlin components | Kotlin.
ERROR @NotNull method org/jetbrains/kotlin/config/CompilerConfigurati
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.asJsr223EvalResult(KotlinJsr223JvmScriptEngineBase.kt:104)

at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:63)

at kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl.compileAndEval(KotlinJsr223ScriptEngineImpl.kt:95)

at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31)

at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)

at kotlinLabGlobal.Interpreter.GlobalValues.initKotlinEngine(GlobalValues.java:468)

at kotlinLabExec.kotlinLab.kotlinLab.<init>(kotlinLab.java:123)

at kotlinLabExec.kotlinLab.kotlinLab.main(kotlinLab.java:240)

Here’s my minimal example with all dependencies:

It works fine for me.

I tested the Kotlin 1.6.21 JSR-223 scripting and it works well when they are not used other libraries.

However, when I use it in KotlinLab, together with other libraries something fails,
while previous Kotlin versions worked well.