Embedding Kotlin in a Java app via JSR 223 API

I have a Java app that supports external scripting via JSR 223 API. I’m currently using Groovy as the main scripting language but I want to add Kotlin as well. I dropped in (what I believe) all the required Kotlin libraries into the app, the app finds and instantiates the Kotlin ScriptEngine and can run simple scripts (e.g., print(“Hello”)) However, when I try to pass objects as script arguments or use other Java classes in the script I get the following error:

“Cannot access script provided property class ‘mypackage.MyClass’. Check your module classpath for missing or conflicting dependencies”
Unresolved reference: myotherpackage

The same app runs the same script fine, if I start it from the IDE (Intellij). It’s only when I run the compiled jar from the command line, things break down. It feels like there is an issues where Kotlin fails to find the classes. I checked the current thread’s ClassLoader before instantiating the ScriptEngine and the ClassLoader seems fine to me – it knows about all the classes including ‘mypackage.MyClass’ and ‘myotherpackage.*’

I’d appreciate any help or suggestions. Thank you.

– Anton

Here are the files I include with the app to get the script engine to load.

trove4j-1.0.20200330.jar
kotlinx-coroutines-core-1.3.9.jar
kotlin-stdlib-1.4.10.jar
kotlin-scripting-jvm-host-1.4.10.jar
kotlin-scripting-jvm-1.4.10.jar
kotlin-scripting-jsr223-1.4.10.jar
kotlin-scripting-compiler-impl-embeddable-1.4.10.jar
kotlin-scripting-compiler-embeddable-1.4.10.jar
kotlin-scripting-common-1.4.10.jar
kotlin-script-util-1.4.10.jar
kotlin-script-runtime-1.4.10.jar
kotlin-reflect-1.4.10.jar
kotlin-daemon-embeddable-1.4.10.jar
kotlin-compiler-embeddable-1.4.10.jar

Hmm… Are your Kts scripts importing the Java classes you’re passing to it?

I’ve used the newer Kotlin Scripting support and haven’t’ used the JSR 223 from the Java side.

Yes, Kotlin fails to find them too: that’s the “Unresolved reference: myotherpackage” error. The Kotlin script imports myotherpackage.SomeClass