The other day I stumbled over Eric Obermühlner’s Java JSR 223 ScriptEngine. java-scriptengine uses the javax ToolProvider SystemJavaCompiler to compile a script into a class in memory, no class file in the file system.
I tried if it would work with openHAB’s JSR 223 engine. I used the Groovy scripting addon as template. My hack soon could load and run simple classes with only JRE imports.
The openHAB JSR 223 engine sets some 70 bindings for the script, which java-scriptengine expects that there are attributes in the classes for each key. I hacked it so that a method setBindings(Map) would be called with all bindings.
If anybody wants to have a look, it’s at GitHub - weberjn/org.openhab.automation.javarules
This is by far not production ready, I did it just to play with the ScriptEngine. Now it would need some more classloader fiddling to access openHAB’s classes to make it useful.
A script in real scripting language gets executed on loading. This does not work for Java, in Java there is no code outside of methods. So one needs an entry method, javarules calls the usual main after the Java script is loaded. So, to do something useful, main would have to register its class in the automation manager.