如何从 Kotlin/Java 中运行 Kotlin-Script (.kts) 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34974039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:36:24  来源:igfitidea点击:

How can I run Kotlin-Script (.kts) files from within Kotlin/Java?

javakotlin

提问by Jire

I noticed that IntelliJ can parse .ktsfiles as Kotlin and the code editor picks them up as free-floating Kotlin files. You are also able to run the script in IntelliJ as you would a Kotlin file with a main method. The script executes from top to bottom.

我注意到 IntelliJ 可以将.kts文件解析为 Kotlin,而代码编辑器会将它们作为自由浮动的 Kotlin 文件来挑选。您还可以在 IntelliJ 中运行脚本,就像使用 main 方法运行 Kotlin 文件一样。脚本从上到下执行。

This form is PERFECT for the project I'm working on, if only I knew an easy way to use them from within Java or Kotlin.

这种形式非常适合我正在从事的项目,如果我知道一种在 Java 或 Kotlin 中使用它们的简单方法就好了。

What's the idiomatic way to "run" these scripts from Java or Kotlin?

从 Java 或 Kotlin“运行”这些脚本的惯用方法是什么?

采纳答案by Alexander Udalov

Note that script files support in Kotlin is still pretty much experimental. This is an undocumented feature which we're still in the process of designing. What's working today may change, break or disappear tomorrow.

请注意,Kotlin 中的脚本文件支持仍处于实验阶段。这是一个未记录的功能,我们仍在设计过程中。今天起作用的东西明天可能会改变、中断或消失。

That said, currently there are two ways to invoke a script. You can use the command line compiler:

也就是说,目前有两种方法可以调用脚本。您可以使用命令行编译器:

kotlinc -script foo.kts <args>

Or you can invoke the script directly from IntelliJ IDEA, by right-clicking in the editor or in the project view on a .kts file and selecting "Run ...":

或者,您可以直接从 IntelliJ IDEA 调用脚本,方法是在编辑器或项目视图中右键单击 .kts 文件并选择“运行...”:

Run .kts from IntelliJ IDEA

从 IntelliJ IDEA 运行 .kts

回答by s1m0nw1

KtsRunner

跑步机

I've published a simple library that let's you run scripts from regular Kotlin programs.

我发布了一个简单的库,让您可以从常规 Kotlin 程序运行脚本。

https://github.com/s1monw1/KtsRunner

https://github.com/s1monw1/KtsRunner

Example

例子

  1. The example class

    data class ClassFromScript(val x: String)
    
  2. The .ktsfile

    import de.swirtz.ktsrunner.objectloader.ClassFromScript
    
    ClassFromScript("I was created in kts")
    
  3. The code to load the class

    val scriptReader =  Files.newBufferedReader(Paths.get("path/classDeclaration.kts"))
    val loadedObj: ClassFromScript = KtsObjectLoader().load<ClassFromScript>(scriptReader)
    println(loadedObj.x) // >> I was created in kts
    
  1. 示例类

    data class ClassFromScript(val x: String)
    
  2. .kts文件

    import de.swirtz.ktsrunner.objectloader.ClassFromScript
    
    ClassFromScript("I was created in kts")
    
  3. 加载类的代码

    val scriptReader =  Files.newBufferedReader(Paths.get("path/classDeclaration.kts"))
    val loadedObj: ClassFromScript = KtsObjectLoader().load<ClassFromScript>(scriptReader)
    println(loadedObj.x) // >> I was created in kts
    

As shown, the KtsObjectLoaderclass can be used for executing a .ktsscript and return its result. The example shows a script that creates an instance of the ClassFromScripttype that is loaded via KtsObjectLoaderand then processed in the regular program.

如图所示,KtsObjectLoader该类可用于执行.kts脚本并返回其结果。该示例显示了一个脚本,该脚本创建了ClassFromScript通过加载KtsObjectLoader并在常规程序中处理的类型的实例。

回答by Dirk Hoffmann

early 2020ies kscriptthat you find at https://github.com/holgerbrandl/kscriptseems to be the most convenient and well supported way to go ...

您在https://github.com/holgerbrandl/kscript 上找到的2020年初 kscript 似乎是最方便、最受支持的方式......