如何从 Kotlin/Java 中运行 Kotlin-Script (.kts) 文件? [英] How can I run Kotlin-Script (.kts) files from within Kotlin/Java?

查看:42
本文介绍了如何从 Kotlin/Java 中运行 Kotlin-Script (.kts) 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这个表单非常适合我正在处理的项目,如果我知道在 Java 或 Kotlin 中使用它们的简单方法就好了.

从 Java 或 Kotlin运行"这些脚本的惯用方式是什么?

解决方案

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

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

kotlinc -script foo.kts 

I noticed that IntelliJ can parse .kts files 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.

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.

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

解决方案

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.

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 ...":

这篇关于如何从 Kotlin/Java 中运行 Kotlin-Script (.kts) 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆