胖子jar中的Kotlin JSR-223 ScriptEngineFactory-无法找到kotlin编译器jar [英] Kotlin JSR-223 ScriptEngineFactory within the fat jar - Cannot find kotlin compiler jar
问题描述
我有一个胖罐子,试图在其中获取Kotlin的ScriptEngine
实例.
I have a fat jar where I'm trying to get the instance of Kotlin's ScriptEngine
.
出于调试目的,我在可用的脚本引擎工厂中进行迭代并获取引擎.
For the debugging purposes I'm iterating through available Script Engine Factories and getting the engines.
val scriptEngineManager = ScriptEngineManager()
for (factory in scriptEngineManager.engineFactories) {
val scriptEngine = factory.scriptEngine
}
当它撞到Kotlin的引擎时,它失败,并带有以下异常:
When it hits the Kotlin's engine, it fails with following exception:
Exception in thread "main" java.io.FileNotFoundException: Cannot find kotlin compiler jar, set kotlin.compiler.jar property to proper location
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt:100)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.getKotlinCompilerJar(KotlinJsr223ScriptEngineFactoryExamples.kt)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.access$getKotlinCompilerJar$p(KotlinJsr223ScriptEngineFactoryExamples.kt:1)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory.getScriptEngine(KotlinJsr223ScriptEngineFactoryExamples.kt:56)
at davidsiro.invoices.InvoiceGeneratorKt.generateInvoice(invoiceGenerator.kt:16)
at davidsiro.invoices.MainKt.main(main.kt:11)
我的胖子罐包含所有依赖项(尽管已解压缩),包括Kotlin编译器.我正在使用Maven Assembly Plugin进行构建,其配置如下:
My fat jar contains all of the dependencies (though unpacked), including Kotlin Compiler. I'm using Maven Assembly Plugin to build it, which configured like these:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
有什么想法吗?
更新
出于记录目的,我尝试对 KotlinJsr223JvmLocalScriptEngineFactory 和 KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory 进行相同的结果.
For the record, I tried to both KotlinJsr223JvmLocalScriptEngineFactory and KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory with the same result.
推荐答案
kotlin-script-util 中的JSR223工厂正在尝试查找编译器jar,以进行编译.在您的情况下,您需要编写自己的工厂来显式提供脚本编译类路径,例如
The JSR223 factories in the kotlin-script-util are trying to find the compiler jar in order to setup the compilation. In your case, you'll need to write your own factory to supply the script compilation classpath explicitly, e.g.
class MyScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
override fun getScriptEngine(): ScriptEngine =
KotlinJsr223JvmLocalScriptEngine(
Disposer.newDisposable(),
this,
classpath, // !!! supply the script classpath here
KotlinStandardJsr223ScriptTemplate::class.qualifiedName!!,
{ ctx, types -> ScriptArgsWithTypes(arrayOf(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), types ?: emptyArray()) },
arrayOf(Bindings::class)
)
}
您需要将以下罐子放入classpath
:
You need to put the following jars into classpath
:
-
kotlin-script-util.jar
-它包含用作脚本超类的模板类 -
kotlin-script-runtime.jar
-用于脚本中使用的基类 - 您需要在脚本中使用的任何其他jar,很有可能-
kotlin-stdlib.jar
kotlin-script-util.jar
- it contains the template class used as a superclass for the scriptkotlin-script-runtime.jar
- for base classes used in scripting- any other jars you'll need to use in your scripts, quite likely -
kotlin-stdlib.jar
您可以将胖子放在那里,但这意味着从脚本中可以访问其中的所有内容.没有谈论编译器的开销.
You may put your fat jar there instead, but that would mean that everything from it will be accessible from your scripts. Not talking about the overheads for the compiler.
这篇关于胖子jar中的Kotlin JSR-223 ScriptEngineFactory-无法找到kotlin编译器jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!