在 java Spring 引导应用程序中执行 Kotlin 脚本
Execute Kotlin scripts inside java Spring Boot Application
我正在尝试在我的 java spring 引导应用程序中执行上面的 kotlin 脚本。
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.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.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.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.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]
我在resourcs/META-INF/services/javax.script.ScriptEngineFactory
下添加了org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
我通过为库添加 kotlin 版本修复了这个错误。
rootProject.kotlinVersion=1.3.72
启动我的 spring 引导应用程序所需的最终依赖项列表是
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")
}
我正在尝试在我的 java spring 引导应用程序中执行上面的 kotlin 脚本。
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.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.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.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.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]
我在resourcs/META-INF/services/javax.script.ScriptEngineFactory
下添加了org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory我通过为库添加 kotlin 版本修复了这个错误。
rootProject.kotlinVersion=1.3.72
启动我的 spring 引导应用程序所需的最终依赖项列表是
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")
}