简体   繁体   中英

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

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?

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?

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?

As of 2020 ( Kotlin 1.3.70 ), you can just use the straightforward

kotlin script.main.kts

Note that using the file extension .main.kts instead of .kts seems to be important .

One of the advantages of Kotlin script is the ability to declare code and dependencies inside a single file (with @file:DependsOn , see for example here )

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM