前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kotlin 使用命令行执行 kts 脚本

Kotlin 使用命令行执行 kts 脚本

作者头像
一个会写诗的程序员
发布2018-08-17 11:34:34
1.9K0
发布2018-08-17 11:34:34
举报
文章被收录于专栏:一个会写诗的程序员的博客

运行 REPL(交互式解释器)

我们可以运行如下命令得到一个可交互的 shell,然后输入任何有效的 Kotlin 代码,并立即看到结果


Kotlin 使用命令行执行 kts 脚本

Kotlin 也可以作为一个脚本语言使用,文件后缀名为 .kts 。

例如我们创建一个名为 list_folders.kts,代码如下:

代码语言:javascript
复制
import java.io.File

val folders = File(args[0]).listFiles { file -> file.isDirectory() }
folders?.forEach { folder -> println(folder) }

执行时通过 -script 选项设置相应的脚本文件。

代码语言:javascript
复制
$ kotlinc -script list_folders.kts <path_to_folder>

参考文章:

https://stackoverflow.com/questions/34974039/how-can-i-run-kotlin-script-kts-files-from-within-kotlin-java

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.12.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 运行 REPL(交互式解释器)
  • Kotlin 使用命令行执行 kts 脚本
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档