我试图在我的java spring boot应用程序中执行以上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$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]
我在resourcs / META-INF / services / javax.script.ScriptEngineFactory下添加了org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
最佳答案
我通过针对库添加kotlin版本来修复此错误。
rootProject.kotlinVersion = 1.3.72
我启动Spring Boot应用程序所需的依赖关系的最终 list 是
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")
}