Kotlin Script (.kts) - How to divide it into multiple files?
Asked Answered
D

1

7

I have a Generator.kts file. When I execute it using:

kotlinc -script Generator.kts

everything works as expected.

However, now my script has grown and I need to separate that class into multiple files.

I did that but when I try to execute it again I get the following errors:

Generator.kts:8:23: error: unresolved reference: CSVReader
        val csvData = CSVReader().readCSV()
                      ^
Generator.kts:10:23: error: unresolved reference: Folders
        val folders = Folders()
                      ^
Generator.kts:14:9: error: unresolved reference: KeyStore
        KeyStore().generateKeyStoreFile(

Basically it fails to find all of the classes I created (CSVReader.kt, Folders.kt and KeyStore.kt). All those classes are in the same folder (including Generator.kts).

How can I run a Kotlin script that uses multiple files?

Dumortierite answered 22/3, 2018 at 21:8 Comment(1)
I think you should have a look at this issue: youtrack.jetbrains.com/issue/KT-11618Rainy
P
9

You could either compile all your sub-scripts into an artifact and add it to the classpath

Or you could use a third party tool like kscript to include them on the fly into your master script.

#!/usr/bin/env kscript

@file:Include("utils.kt")

val robustMean = listOf(1.3, 42.3, 7.).median()
println(robustMean)

For details and examples see https://github.com/holgerbrandl/kscript#ease-prototyping-with-include

Disclaimer: I'm a contributor of kscript.

Petty answered 6/6, 2018 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.