Caused by: javax.script.ScriptException: Unresolved reference: Import
TomasRup opened this issue · 3 comments
TomasRup commented
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")
TomasRup commented
I figured it out, I had to use .main.kts extension/engine.
hussainmoatsim commented
I figured it out, I had to use
.main.ktsextension/engine.
Hello, do you mind telling me how exactly you use that?
TWiStErRob commented
<foo>.main.kts filename, see https://github.com/Kotlin/kotlin-script-examples/tree/master/jvm/jsr223/jsr223-main-kts/testData
and
.getEngineByExtension("main.kts"), see https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/jsr223/jsr223-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/mainKts/repl.kt