Can I run kotlin as script with Java Scripting API

后端 未结 2 760
小鲜肉
小鲜肉 2021-01-05 13:13

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 {
    pu         


        
相关标签:
2条回答
  • 2021-01-05 13:50

    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.

    0 讨论(0)
  • 2021-01-05 13:51

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

    This configuration 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>

    0 讨论(0)
提交回复
热议问题