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

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

问题描述

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

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.

这个表单对于我正在处理的项目来说是完美的,只要我知道在Java中使用它们的简单方法或者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?

推荐答案

请注意,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>

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

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天全站免登陆