Embedding Kotlin as scripting language in Java apps

Hi,
I’m interested in using Kotlin as new embedded scripting language in our jAlbum app (http://jalbum.net), but I see no obvious support for Oracle’s javax.script API. It would be great to have drop-in support for adding Kotlin as embedded scripting language just like Jython and Groovy provides (just add the right jar file to the classpath). Is support for this in the workings? If so, any timeline and estimates on disk footprint?

Regards
/David (Founder and lead developer of Java based image gallery app jAlbum (http://jalbum.net))

The support for javax.scripting is available in the current milestone release of Kotlin 1.1 and will also be included in its final release.

1 Like

Great news! Any time estimates on the final release? What disk footprint can I basically expect? (+/- 1 MB is ok accuracy)

Kotlin 1.1 is planned for Q1 2017. Its size will not be significantly different from the size of the existing 1.1-M04 release.

Sorry for asking this (not friend with Maven): Are we talking the range of 20MB or <5MB?

I wrote a little java program using Eclipse to enumerate all available scripting languages - I added all availabe jars from Kotlin 1.1.1 to the project - but no Kotlin scripting engine was found.

	ScriptEngineManager mgr = new ScriptEngineManager();
	
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();

    for (ScriptEngineFactory factory : factories) {

        System.out.println("ScriptEngineFactory Info");

        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();

        System.out.printf("\tScript Engine: %s (%s)%n", engName, engVersion);

        List<String> engNames = factory.getNames();
        for(String name : engNames) {
            System.out.printf("\tEngine Alias: %s%n", name);
        }

        System.out.printf("\tLanguage: %s (%s)%n", langName, langVersion);

    }

See http://kotlinlang.org/docs/reference/whatsnew11.html#javaxscript-support for information on using the API.

I tried running that little embedded Kotlin example using jAlbum’s scripting console and received the following error:

java.lang.IncompatibleClassChangeError: com/xafero/dynkt/core/MemoryScriptDefinition
in inline evaluation of: ``e.eval(“val x = 3”);‘’ at line number 1

Moreover, the following debug print is also generated:
81084 [AWT-EventQueue-0] INFO kc - Compiling ‘/var/folders/hc/55gr2vw145b0m7cpllskd0ph0000gn/T/kc_evl4159402832513912920.kts’…

This shows that Kotlin writes stuff to the file system for each script execution. Not good.

Regards
/David

For future searchers finding this topic, the key steps to get it to work are:

You need these libraries in your class path (in additon to stdlib):

  • kotlin-compiler
  • kotlin-script-util

In addition, you need a META-INF/services/javax.script.ScriptEngineFactory file in the classpath jar to tell the JSR223 script engine to use the Kotlin script engine. Just adding the above libraries does not do it (probably a security concern). The easiest way to just add the file to your own jar like from this example:

4 Likes

Hi,
I am trying to use the jsr223 support.
When the engine fail to evaluate due to unresolved reference - it will forever fail to evaluate new expressions, i.e.,:

val engine = KotlinJsr223JvmLocalScriptEngineFactory().scriptEngine
println(engine.eval("1+1")); // prints 2

try {
    println(engine.eval("a")); // fail with unresolved identifier exception
} catch (err: Throwable) {...}

println(engine.eval("1+1")); // fail again with exception: rg.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration Line_3 Cause: Could not find a classifier for File name: Line_3.kts Physical: true Injected: false


Is there any way to salvage the engine (its context) and continue working after such error?

I can run scripts if I use the IDE but when the application is packaged into a jar it no longer finds the Kotlin Script engine. When I try to list the script engines it does show Kotlin but it is not there when I try to get it by extension. Any suggestions?

This answer fixed my problem. I also had a fatJar.