JSR-223 integration


#1

I’m trying to integrate kotlin as a JSR-223 scripting language but I’m having some problem

Specifically, I’m trying to use it in Coldfusion (just for fun)

I’d like to be able to do something like

createObject(‘java’, ‘javax.script.ScriptEngineManager’).getEngineByExtension(“kts”).eval(“var x = 2 + 3”)

So far I’ve been able to get it to register as a jsr223 engine, but I get this error

Cannot load extensions/common.xml from C:\srv\ColdFusion11\cfusion\lib\kotlin-compiler.jar

I’ve tried many variants with different kind of errors, plus I had to remove log4j from kotlin-compiler.jar because of clashes with embedded log4j inside Coldfusion.

Right now I’ve this list of jars added to CF

"C:\srv\ColdFusion11\cfusion\lib\kotlin-compiler.jar"
"C:\srv\ColdFusion11\cfusion\lib\kotlin-reflect.jar"
"C:\srv\ColdFusion11\cfusion\lib\kotlin-runtime.jar"
"C:\srv\ColdFusion11\cfusion\lib\kotlin-script-runtime.jar"
"C:\srv\ColdFusion11\cfusion\lib\kotlin-script-util-1.1.2-2.jar"
"C:\srv\ColdFusion11\cfusion\lib\kotlin-stdlib.jar"

but I’m totally unsure of the right minimum set of dependencies to make it run.

Is there a “right” way of doing this?


#2

The best place to start is this sample project - https://github.com/JetBrains/kotlin/tree/master/libraries/examples/kotlin-jsr223-local-example.

If you have any clashes with the environment, you can switch to compiler-embeddable artefact, in which all potentially conflicting parts are shadowed. But in this case you’ll need to make your own ScriptEngineFactory, similar to this one - https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223ScriptEngineFactoryExamples.kt#L37 - but using embeddable compiler jar instead of the regular one (see kotlinCompilerJar function in that file.)

I cannot yet think of any reason for the error you mention. If you’ll continue to get it, please share a callstack.


#3

I’m trying again, I’ve updated kotlin libs, and have been able to make some progress.

Now I’m stuck with this error

java.lang.ExceptionInInitializerError
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns.createBuiltInsModule(KotlinBuiltIns.java:139)
        at org.jetbrains.kotlin.platform.JvmBuiltIns.<init>(JvmBuiltIns.kt:56)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createModuleContext(TopDownAnalyzerFacadeForJVM.kt:284)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createContainer(TopDownAnalyzerFacadeForJVM.kt:128)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createContainer$default(TopDownAnalyzerFacadeForJVM.kt:125)
        at org.jetbrains.kotlin.cli.jvm.repl.ReplCodeAnalyzer.<init>(ReplCodeAnalyzer.kt:62)
        at org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompilerState.<init>(GenericCompilerState.kt:71)
        at org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler.createState(GenericReplCompiler.kt:51)
        at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluator.createState(GenericReplCompilingEvaluator.kt:31)
        at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.createState(KotlinJsr223JvmLocalScriptEngine.kt:60)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.createState$default(KotlinJsr223JvmScriptEngineBase.kt:46)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.getCurrentState(KotlinJsr223JvmScriptEngineBase.kt:53)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.nextCodeLine(KotlinJsr223JvmScriptEngineBase.kt:44)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:73)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:35)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at coldfusion.runtime.StructBean.invoke(StructBean.java:508)
        at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2553)
        at cfscript2ecfm1154740214$funcRUNSCRIPT.runFunction(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\engine\script.cfm:105)
        at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:487)
        at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
        at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:420)
        at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:383)
        at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:95)
        at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:334)
        at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:231)
        at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2845)
        at cfscript2ecfm1154740214.runPage(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\engine\script.cfm:139)
        at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
        at coldfusion.filter.CFVariablesScopeFilter.invoke(CFVariablesScopeFilter.java:56)
        at coldfusion.tagext.lang.ModuleTag.doAfterBody(ModuleTag.java:413)
        at cfk2ecfm204221519.runPage(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\k.cfm:3)
        at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
        at coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:736)
        at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:572)
        at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
        at coldfusion.filter.IpFilter.invoke(IpFilter.java:45)
        at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487)
        at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42)
        at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
        at coldfusion.filter.PathFilter.invoke(PathFilter.java:142)
        at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30)
        at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
        at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:78)
        at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
        at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
        at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58)
        at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
        at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
        at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
        at coldfusion.CfmServlet.service(CfmServlet.java:219)
        at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
        at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter(MobileDeviceDomInspectionFilter.java:121)
        at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:450)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
java.util.NoSuchElementException: Collection is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:166)
    at org.jetbrains.kotlin.builtins.BuiltInsLoader$Companion.<init>(BuiltInsLoader.kt:38)
    at org.jetbrains.kotlin.builtins.BuiltInsLoader$Companion.<init>(BuiltInsLoader.kt:36)
    at org.jetbrains.kotlin.builtins.BuiltInsLoader.<clinit>(BuiltInsLoader.kt)
    at org.jetbrains.kotlin.builtins.KotlinBuiltIns.createBuiltInsModule(KotlinBuiltIns.java:139)
    at org.jetbrains.kotlin.platform.JvmBuiltIns.<init>(JvmBuiltIns.kt:56)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createModuleContext(TopDownAnalyzerFacadeForJVM.kt:284)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createContainer(TopDownAnalyzerFacadeForJVM.kt:128)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.createContainer$default(TopDownAnalyzerFacadeForJVM.kt:125)
    at org.jetbrains.kotlin.cli.jvm.repl.ReplCodeAnalyzer.<init>(ReplCodeAnalyzer.kt:62)
    at org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompilerState.<init>(GenericCompilerState.kt:71)
    at org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler.createState(GenericReplCompiler.kt:51)
    at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluator.createState(GenericReplCompilingEvaluator.kt:31)
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.createState(KotlinJsr223JvmLocalScriptEngine.kt:60)
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.createState$default(KotlinJsr223JvmScriptEngineBase.kt:46)
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.getCurrentState(KotlinJsr223JvmScriptEngineBase.kt:53)
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.nextCodeLine(KotlinJsr223JvmScriptEngineBase.kt:44)
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:73)
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compile(KotlinJsr223JvmScriptEngineBase.kt:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at coldfusion.runtime.StructBean.invoke(StructBean.java:508)
    at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2553)
    at cfscript2ecfm1154740214$funcRUNSCRIPT.runFunction(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\engine\script.cfm:105)
    at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:487)
    at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
    at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:420)
    at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:383)
    at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:95)
    at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:334)
    at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:231)
    at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2845)
    at cfscript2ecfm1154740214.runPage(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\engine\script.cfm:139)
    at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
    at coldfusion.filter.CFVariablesScopeFilter.invoke(CFVariablesScopeFilter.java:56)
    at coldfusion.tagext.lang.ModuleTag.doAfterBody(ModuleTag.java:413)
    at cfk2ecfm204221519.runPage(C:\srv\ColdFusion11\cfusion\wwwroot\cfgroovy\k.cfm:3)
    at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
    at coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:736)
    at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:572)
    at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
    at coldfusion.filter.IpFilter.invoke(IpFilter.java:45)
    at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487)
    at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42)
    at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
    at coldfusion.filter.PathFilter.invoke(PathFilter.java:142)
    at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30)
    at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
    at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:78)
    at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
    at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
    at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58)
    at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
    at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
    at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
    at coldfusion.CfmServlet.service(CfmServlet.java:219)
    at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter(MobileDeviceDomInspectionFilter.java:121)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:450)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

It happens when I call

engine.compile(body)

where body is the actual script I’m trying to compile

Here’s the coldfusion code, just for reference:

<cfimport prefix="g" taglib="engine" />

<g:script lang="kotlin">val x: String = "OK"</g:script>

#4

A little bit of additional information: I’m using a coldfusion library that loads an isolated classloader, and loading a limited set of jars, specifically:

kotlin-stdlib.jar
kotlin-compiler.jar
kotlin-compiler-client-embeddable.jar
kotlin-reflect.jar
kotlin-script-runtime.jar
kotlin-script-util-1.1.4-2.jar

In this moment I have

kotlin.compiler.jar = "kotlin-compiler.jar"

and I obtain the engine this way:

<cfset var o = javaLoader.create("org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine").init()>
<cfset var engine = o.getScriptEngine()>