# Trying to use Kotlin via javax.script (JSR 223) in a webapp

**URL:** <https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982>\
**Category:** Support\
**Created:** [August 9, 2018, 9:25pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982 "2018-08-09T21:25:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ujay68](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@ujay68](https://discuss.kotlinlang.org/u/ujay68)\
**Post date:** [August 9, 2018, 9:25pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/1 "2018-08-09T21:25:51Z")

</div>

Hi, I’m trying to use Kotlin (v1.2.60) packaged in a .war file deployed to WildFly (v13.0.0.Final), to be accessed through the javax.script API (JSR 223). WEB-INF/lib contains the jars from mvn packages “org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.60” and “org.jetbrains.kotlin:kotlin-script-util:1.2.60”.

Trying a simple eval like this:

```
ScriptEngine e = new KotlinJsr223JvmLocalScriptEngineFactory().getScriptEngine();
e.eval("1+1");

```

Gives this stacktrace:

```
java.lang.IllegalStateException: Resource not found: /kotlin/jvm/JvmStatic.class
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.impl.PathUtilKt.getResourcePathForClass(pathUtil.kt:76)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.KotlinJars.getLib(context.kt:147)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.KotlinJars$stdlibOrNull$2.invoke(context.kt:151)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.KotlinJars$stdlibOrNull$2.invoke(context.kt:119)
	at deployment.kotlin_script_test.war//kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.KotlinJars.getStdlibOrNull(context.kt)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.KotlinJars.getKotlinScriptStandardJars(context.kt:168)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.ContextKt.scriptCompilationClasspathFromContextOrStlib(context.kt:109)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.util.ContextKt.scriptCompilationClasspathFromContextOrStlib$default(context.kt:106)
	at deployment.kotlin_script_test.war//org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory.getScriptEngine(KotlinJsr223ScriptEngineFactoryExamples.kt:46)
	at deployment.kotlin_script_test.war//kotlin.KotlinEvaluator.eval(KotlinEvaluator.java:10)

```

Running the code in a standalone app outside a web container with the same dependencies works fine.

Any ideas? What am I missing? Is the Kotlin script engine ready to be run inside a webapp?

Thanks, John

---

<div class="post-metadata">

**Author:** ![ujay68](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@ujay68](https://discuss.kotlinlang.org/u/ujay68)\
**Post date:** [August 10, 2018, 9:28pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/2 "2018-08-10T21:28:05Z")

</div>

I’ve tried to debug this and found that the method `extractRoot` from libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/impl/pathUtil.kt is called with an URL like this:

`"vfs:/PATH_TO_EXPLODED_WAR/WEB-INF/lib/kotlin-stdlib-1.2.60.jar/kotlin/jvm/JvmStatic.class"`

where “vfs” seems to be a special WildFly Virtual File System protocol which the `extractRoot` method cannot handle because it’s hard-wired to “jar” and “file”.

Any recommendations?

---

<div class="post-metadata">

**Author:** ![ilya.chernikov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.chernikov/32/1455_2.png) [@ilya.chernikov](https://discuss.kotlinlang.org/u/ilya.chernikov)\
**Post date:** [August 20, 2018, 12:11pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/3 "2018-08-20T12:11:13Z")

</div>

Seems this is an untested path. We’ll do something about it, but meanwhile, you can workaround it by specifying script compilation classpath explicitly via `kotlin.compiler.classpath` property. You’ll need to put at least `kotlin-script-util` there, and for any useful script, quite likely `kotlin-stdlib` as well.

---

<div class="post-metadata">

**Author:** ![ujay68](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@ujay68](https://discuss.kotlinlang.org/u/ujay68)\
**Post date:** [August 21, 2018, 7:09am UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/4 "2018-08-21T07:09:15Z")

</div>

Thanks! I’ve added the jars to `kotlin.compiler.classpath` one after the other, getting various exceptions on the way. But with this classpath:

`-Dkotlin.compiler.classpath=kotlin-stdlib.jar:kotlin-script-util.jar:kotlin-stdlib-common.jar:kotlin-script-runtime.jar:kotlin-compiler-embeddable.jar`

I’m stuck with this:

```
ERROR [stderr] cannot extract: /org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment$Companion.class from vfs:/content/kotlin_script_test.war/WEB-INF/lib/kotlin-compiler-embeddable-1.2.60.jar/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment$Companion.class
ERROR [io.undertow.request] UT005023: Exception handling request to /kotlin_script_test/: java.lang.IllegalStateException: Resource not found: /org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment$Companion.class

```

I would be grateful if you could take this up in some future release.

---

<div class="post-metadata">

**Author:** ![achekler](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@achekler](https://discuss.kotlinlang.org/u/achekler)\
**Post date:** [November 20, 2018, 7:20am UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/5 "2018-11-20T07:20:46Z")

</div>

Hi, I am getting exactly the same errors in v1.3.10 trying to do the same in OSGi runtime (equinox). Script Engine can be created with the compiler classpath workaround, but the errors come when I try to execute a script.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [November 20, 2018, 4:13pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/6 "2018-11-20T16:13:17Z")

</div>

The kotlin script support is implemented by compiling the code at runtime and then executing the resulting code. It means you need access to the Kotlin compiler at runtime. It also means that this is not the fastest thing in the world.

---

<div class="post-metadata">

**Author:** ![ujay68](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@ujay68](https://discuss.kotlinlang.org/u/ujay68)\
**Post date:** [March 9, 2019, 9:58pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/7 "2019-03-09T21:58:33Z")

</div>

@ilya.chernikov Is there maybe any news here on this topic? Still very much interested in using Kotlin as a script engine inside a web container.

---

<div class="post-metadata">

**Author:** ![ujay68](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@ujay68](https://discuss.kotlinlang.org/u/ujay68)\
**Post date:** [March 9, 2019, 10:04pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/8 "2019-03-09T22:04:11Z")

</div>

IMO, this is exactly what the javax.script mechanism is for. There are situations where you cannot (or prefer not to) package everything at compile-time but deploy certain parts of your code or configuration-as-code at runtime. Using Kotlin, you’d of course have a small performance penalty when that code is run for the first time, but after that it will run and interoperate the same as pre-compiled code.

---

<div class="post-metadata">

**Author:** ![ilya.chernikov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.chernikov/32/1455_2.png) [@ilya.chernikov](https://discuss.kotlinlang.org/u/ilya.chernikov)\
**Post date:** [March 11, 2019, 8:03am UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/9 "2019-03-11T08:03:21Z")

</div>

Unfortunately no news so far. I’ve created an issue for easier tracking - [https://youtrack.jetbrains.com/issue/KT-30367](https://youtrack.jetbrains.com/issue/KT-30367). We’ll have a look, what could be done about it.

The original issue is that kotlin is not yet able to compile with classes directly from current classloader (as many interpreted languages like groovy and clojure can), so it needs to get a classpath in a form that the compiler will be able to use. And in a webapp environment this is not trivial.  
There is an issue about this - [https://youtrack.jetbrains.com/issue/KT-27956](https://youtrack.jetbrains.com/issue/KT-27956) - we’re going to address it in some not too distant future. And then these problems should go away.

---

<div class="post-metadata">

**Author:** ![davidpcaldwell](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@davidpcaldwell](https://discuss.kotlinlang.org/u/davidpcaldwell)\
**Post date:** [May 22, 2019, 10:30pm UTC](https://discuss.kotlinlang.org/t/trying-to-use-kotlin-via-javax-script-jsr-223-in-a-webapp/8982/10 "2019-05-22T22:30:35Z")

</div>

Other readers please note: I needed to use the `kotlin.script.classpath`, rather than `kotlin.compiler.classpath`, to get this to work.
