Groovy Rules and Java 9+

This is just to share my experiences trying to migrate my old OpenHABian instance running on java 8 for ages to a new one, running on java 11.
If you wonder why the Groovy ScriptEngine isn’t loaded anymore (resulting in ScriptEngine for groovy not available messages) here’s why:
The groovy librarys (the ones extracted from the Groovy SDK zip) go usually to the runtime/lib/ext directory and are loaded by the JVM from there.
From Java 9 onwoards the whole mechanism of loading Extensions has been removed. (Refer to https://docs.oracle.com/javase/9/migrate/toc.htm#GUID-D867DCCC-CEB5-4AFA-9D11-9C62B7A3FAB1 “Removed Extension mechanism”). The advice is to just add JARs to the regular classpath, which worked for me (using * wildcard).

I did this by adding 2 lines to runtime/bin/karaf:
JAVA_9_ADD_CP_DIR="${KARAF_HOME}/lib/groovy/*" after the line JAVA_EXT_DIRS=
and I changed the -classpath ... to the following in the if [ "${VERSION}" -gt "8" ]; then section:
-classpath "${CLASSPATH}:${JAVA_9_ADD_CP_DIR}" \

Then I moved all groovy jars (read: all jars in the directory) from runtime/lib/ext to a newly created runtime/lib/groovy and the scripts got loaded again.

HTH

1 Like

I believe openHAB 3.0.0M2 has Groovy available for scripting with Java 11. OH2 is designed only for Java 8.

I used the latest OpenHABian image (2.5.10) and it ran java 11 by default (which worked).
Also, doing this adoptions, the Groovy rules seem to work, so all good (apart from a little bit of investigating ;-))
I didn’t want to migrate to an OH3 milestone yet (mostly because the OpenHABian image isn’t ready).

1 Like