Hi,
I’m working on integrating Kotlin scripting support into an OSGi environment. The project is Maven-based, and I’ve included all the necessary dependencies for .kts
script execution. Currently, I’m using a shaded (fat) JAR to package everything, though ideally I’d prefer a more modular or lightweight setup if available.
Here’s the current list of dependencies:
<!-- Kotlin dependencies for .kts execution -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-osgi-bundle</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-runtime</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-common</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jvm</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jvm-host</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-compiler-embeddable</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jsr223</artifactId>
<version>${kotlin.version}</version>
</dependency>
To execute scripts, I’m using the following logic to initialize the Kotlin script engine:
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
ScriptEngineManager manager = new ScriptEngineManager();
this.kotlinEngine = manager.getEngineByName("kotlin");
if (this.kotlinEngine != null) {
LOG.info("OK: Kotlin ScriptingHost activated successfully using standard ScriptEngineManager.");
} else {
LOG.error("CRITICAL: Kotlin script engine not found. The embeddable dependency may be missing.");
}
} catch (Throwable t) {
LOG.error("CRITICAL: A fatal error occurred during ScriptingHost activation.", t);
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
However, when executing a Kotlin script
this.engine.eval(this.scriptText, context);
, I get the following error:
javax.script.ScriptException: ERROR Unable to initialize repl compiler:
DEBUG Using JDK home inferred from java.home: /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
ERROR Unable to find kotlin stdlib, please specify it explicitly via “kotlin.java.stdlib.jar” property: java.lang.Exception: Unable to find kotlin stdlib, please specify it explicitly via “kotlin.java.stdlib.jar” property: java.lang.IllegalStateException: Unable to initialize repl compiler:
…
This is confusing, as the kotlin-stdlib
is already included and correctly listed in the bundle’s classpath. The runtime shouldn’t need to be told explicitly where the kotlin-stdlib.jar
is located if it’s already part of the embedded bundle.
Any guidance on how to address this? Specifically:
- Is there a way to declaratively satisfy the
kotlin.java.stdlib.jar
requirement within an OSGi bundle? - Is this a classloader visibility issue inside the scripting engine?
- Are there lighter alternatives to bundling the full compiler?
Thanks in advance for your help!