Groovy has a GroovyClassLoader with which a groovy class can be loaded into your java application with GroovyClassLoader.parseClass(String script) and then instantiated at a later time and it's method invoked.
Is there a way to do this with kotlin script files (.kts)?
I know I can use jsr223 to evaluate a script file with ScriptEngine.eval(String script) which uses KotlinJsr223JvmLocalScriptEngineFactory , however calling eval over a script which is simply a class declaration, returns null and I can't access that class.
I have added the correct dependencies and kotlin script engine factory is correctly loaded (I can see it in the result of new ScriptEngineManager().getEngineFactories()), I just get null as a result of eval. Assuming that's because the script itself doesn't really return anything, just define a class. This is what the script file I'm trying to load looks like:
package scripts
import com.example.SomeJavaInterface
import com.example.SomeArg
import com.example.SomeReturnValue
class TestScript : SomeJavaInterface {
override fun someMethod(SomeArg arg): SomeReturnValue {
return SomeReturnValue()
}
}
I'm using kotlin-stdlib, kotlin-compiler and kotlin-script-util version 1.2.41.
Is this not achievable with kotlin scripts?
IllegalStateException: Could not load script from .kts. Now From what I can see the library an eval and then tries to cast the result and the thing breaks because eval returns null, just like it does to me in my original eval call.new KtsObjectLoader().load(Files.newBufferedReader(Paths.get("script.kts")))or callingscriptEngine.eval(script)both return null. Script file is properly loaded (I can see it's contents when debugging).