Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run kotlin as script with Java Scripting API

I want to run kotlin code as script from java with Java Scripting API similar to this for javascript:

import javax.script.*;
public class EvalScript {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        // evaluate JavaScript code from String
        engine.eval("print('Hello, World')");
    }
}

or with similar APIs like this.

like image 629
mojtab23 Avatar asked Jul 31 '16 18:07

mojtab23


People also ask

Can Kotlin be used with JavaScript?

Kotlin for JavaScript Kotlin/JS provides the ability to transpile your Kotlin code, the Kotlin standard library, and any compatible dependencies to JavaScript. The current implementation of Kotlin/JS targets ES5.

Does Kotlin use Java or JavaScript?

Kotlin is a statically-typed programming language that runs on the Java Virtual Machine and also can be compiled to JavaScript source code.

Is Kotlin good for scripting?

Kotlin makes scripting really easy. Powerful scripts can be created with the autocompletion and the strong typing, making them really readable and highly maintainable.


2 Answers

Yes, it's possible starting from the Kotlin 1.1: http://kotlinlang.org/docs/reference/whatsnew11.html#javaxscript-support

KEEP-75 has an example of the JSR-223 API call:

val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
engine.eval("""
@file:DependsOn("junit:junit:4.11")

org.junit.Assert.assertTrue(true)

println("Hello, World!")
""")

Configuration below adds Kotlin scripts engine to my Kotlin 1.2 project:

  • META-INF/services/javax.script.ScriptEngineFactory file with the content from the https://github.com/JetBrains/kotlin/blob/master/libraries/examples/kotlin-jsr223-local-example/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory

  • 2 libraries:

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-script-runtime</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-script-util</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

Update: Starting from Kotlin 1.2.20 kotlin-script-util doesn't depend on kotlin-compiler explicitly (see https://youtrack.jetbrains.com/issue/KT-17561). So one more module should be provided (as of the build file in example project):

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-compiler-embeddable</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
like image 149
Lu55 Avatar answered Sep 19 '22 04:09

Lu55


Kotlin support for the Java Scripting API is planned, but as of version 1.0.3 is not available yet. For the mean time, you can try to use an existing open-source implementation.

like image 45
yole Avatar answered Sep 21 '22 04:09

yole