Kotlin/kotlin-script-examples

Caused by: javax.script.ScriptException: Unresolved reference: Import

TomasRup opened this issue · 3 comments

Hey, what am I doing wrong, if I cannot import in my kts files?

Some pseudocode:

otherfolder/otherfile.kts

fun otherFn() {
  // ...
}

main.kts

@file:Import("otherfolder/otherfile.kts")

fun main() {
  otherFn()
}

I am getting:

Caused by: javax.script.ScriptException: Unresolved reference: Import
Unresolved reference: Import
Unresolved reference: otherFn

This is how I load them in my kotlin/spring app:

        val scriptEngine = ScriptEngineManager().getEngineByExtension("kts")
        val fileReader = FileReader(Paths.get("src/main/resources/main.kts").toFile())
        scriptEngine.eval(fileReader)
        return scriptEngine as Invocable
    kotlin("jvm") version "1.4.21"
    // ...
    implementation( "org.jetbrains.kotlin:kotlin-script-runtime")
    implementation( "org.jetbrains.kotlin:kotlin-script-util")
    implementation( "org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable")
    implementation( "org.jetbrains.kotlin:kotlin-scripting-jvm-host")
    implementation( "org.jetbrains.kotlin:kotlin-scripting-jsr223")

I figured it out, I had to use .main.kts extension/engine.

I figured it out, I had to use .main.kts extension/engine.

Hello, do you mind telling me how exactly you use that?