Выполнить Kotlin скрипты внутри java Spring Boot Application - PullRequest
0 голосов
/ 18 апреля 2020

Я пытаюсь выполнить выше kotlin скрипт внутри моего java приложения для весенней загрузки.

public class ScriptExectuorServImpl {

    public void execute() throws ScriptException {

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByExtension("kts");
    engine.eval(("println('OK')"));

    }

}

Мои зависимости gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtime 'org.jetbrains.kotlin:kotlin-script-util'
    runtime 'org.jetbrains.kotlin:kotlin-compiler-embeddable'
    runtime 'org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable'
}

Я сталкиваюсь с нижеприведенным ошибка

java.lang.NoClassDefFoundError: org/jetbrains/kotlin/scripting/compiler/plugin/repl/GenericReplCompiler
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$replCompiler$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:47) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$replCompiler$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:38) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) ~[kotlin-stdlib-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getReplCompiler(KotlinJsr223JvmLocalScriptEngine.kt) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$localEvaluator$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:53) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine$localEvaluator$2.invoke(KotlinJsr223JvmLocalScriptEngine.kt:38) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) ~[kotlin-stdlib-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getLocalEvaluator(KotlinJsr223JvmLocalScriptEngine.kt) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.getReplEvaluator(KotlinJsr223JvmLocalScriptEngine.kt:55) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine.createState(KotlinJsr223JvmLocalScriptEngine.kt:59) ~[kotlin-script-util-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.createState$default(KotlinJsr223JvmScriptEngineBase.kt:46) ~[kotlin-compiler-embeddable-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.getCurrentState(KotlinJsr223JvmScriptEngineBase.kt:53) ~[kotlin-compiler-embeddable-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.nextCodeLine(KotlinJsr223JvmScriptEngineBase.kt:44) ~[kotlin-compiler-embeddable-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:61) ~[kotlin-compiler-embeddable-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31) ~[kotlin-compiler-embeddable-1.3.71.jar:1.3.71-release-431 (1.3.71)]
    at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:na]
    at com.example.scriptexecutor.service.ScriptExectuorServImpl.execute(ScriptExectuorServImpl.java:15) ~[main/:na]

Я добавил org.jetbrains. kotlin .script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory в разделе resourcs / META-INF / services / javax.script.ScriptEngineFactory

1 Ответ

0 голосов
/ 21 апреля 2020

Я исправил эту ошибку, добавив kotlin версию к библиотекам.

rootProject.kotlinVersion = 1.3.72

Мой окончательный список зависимостей, необходимых для запуска моего весеннего загрузочного приложения:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-script-runtime:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-script-util:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-jvm-host-embeddable:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-jvm-host:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-jvm:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-jsr223-embeddable:${rootProject.kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-scripting-jsr223:${rootProject.kotlinVersion}"
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}
...