代码之家  ›  专栏  ›  技术社区  ›  David Siro

fat jar中的Kotlin JSR-223 ScriptEngineFactory-找不到Kotlin编译器jar

  •  5
  • David Siro  · 技术社区  · 7 年前

    ScriptEngine .

    出于调试目的,我正在遍历可用的脚本引擎工厂并获取引擎。

    val scriptEngineManager = ScriptEngineManager()
    for (factory in scriptEngineManager.engineFactories) {
        val scriptEngine = factory.scriptEngine
    }
    

    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)
    

    我的fat jar包含所有依赖项(尽管未打包),包括Kotlin编译器。我正在使用Maven Assembly插件构建它,其配置如下:

    <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

    1 回复  |  直到 6 年前
        1
  •  9
  •   mkobit    6 年前

    日本JSR223工厂 kotlin脚本util

    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