kscript icon indicating copy to clipboard operation
kscript copied to clipboard

Please document how to add modules

Open DanySK opened this issue 3 years ago • 1 comments

Hi, after lurking the old issues and trying several options, I still could not find how to add modules. I see that the issues there and under Kotlin are now solved, but I can find no documentation nor working examples. Could you document how to do so?

e.g., I expected:

@file:KotlinOpts("-J'--add-modules java.scripting'")
println(ScriptEngineManager())

to work, but it does not.

DanySK avatar Feb 09 '22 12:02 DanySK

The pure kotlin way is

kotlin -howtorun script -J'--add-modules' -J'java.scripting' my_script.kts

with my_script.kts being

import javax.script.ScriptEngineManager

println(ScriptEngineManager())

Using kscript this should translate to (see https://discuss.gradle.org/t/gradle-does-not-honor-add-modules-jvm-argument-in-jdk9/20403)

@file:KotlinOpts("-J'--add-modules' -J'java.scripting'")
//@file:CompilerOpts("-J'--add-modules' -J'java.scripting'")

import javax.script.ScriptEngineManager

println(ScriptEngineManager())

but this fails with Caused by: java.lang.ClassNotFoundException: javax.script.ScriptEngineManager. I don't know why, but given the history, I'd suspect the kotlin scripting classloader to not handle modules correctly. But this is just a weak guess.

Sorry for the inconvenience.

holgerbrandl avatar Apr 19 '22 18:04 holgerbrandl