Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kotlin

kotlinc

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?

like image 667
Bitcoin Cash - ADA enthusiast Avatar asked Mar 22 '18 21:03

Bitcoin Cash - ADA enthusiast


1 Answers

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.

like image 111
Holger Brandl Avatar answered Nov 11 '22 07:11

Holger Brandl