我有一个胖罐子,我正试图获得Kotlin ScriptEngine
的实例。
出于调试目的,我正在迭代可用的脚本引擎工厂并获取引擎。
val scriptEngineManager = ScriptEngineManager()
for (factory in scriptEngineManager.engineFactories) {
val scriptEngine = factory.scriptEngine
}
当它击中Kotlin的引擎时,它失败并出现以下异常:
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来构建它,其配置如下:
<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 ,结果相同。
答案 0 :(得分:7)
kotlin-script-util 中的JSR223工厂正在尝试查找编译器jar以设置编译。在您的情况下,您需要编写自己的工厂以明确提供脚本编译类路径,例如。
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
:
kotlin-script-util.jar
- 它包含用作脚本kotlin-script-runtime.jar
- 用于编写脚本的基类kotlin-stdlib.jar
你可以把你的胖罐放在那里,但这意味着可以从你的脚本中访问它的所有内容。不是在谈论编译器的开销。