How to point OH2 to a different java version

I was wondering how to point OH2 to a different java version. With OH1.x, you could easily point to other binaries from start.sh/bat or within the launch scripts in init.d. How to do this with OH2?

Love to answer my own questions… :wink: Just put alternating java versions wherever you want and fix the path like this:
update-alternatives --install “/usr/bin/java” “java” “/opt/java/hlft/jdk1.8.0_101/bin/java” 1
update-alternatives --install “/usr/bin/javac” “javac” “/opt/java/hlft/jdk1.8.0_101/bin/javac” 1
update-alternatives --install “/usr/bin/javaws” “javaws” “/opt/java/hlft/jdk1.8.0_101/bin/javaws” 1
update-alternatives --install “/usr/bin/jar” “jar” “/opt/java/hlft/jdk1.8.0_101/bin/jar” 1
update-alternatives --set “java” "/opt/java/hlft/jdk1.8.0_101/bin/java"
update-alternatives --set “javac” "/opt/java/hlft/jdk1.8.0_101/bin/javac"
update-alternatives --set “javaws” "/opt/java/hlft/jdk1.8.0_101/bin/javaws"
update-alternatives --set “jar” “/opt/java/hlft/jdk1.8.0_101/bin/jar”

In OH2.1 I recently did the following:

Download java from oracle and extract to /opt.
In my case this was jdk-8u152-linux-arm32-vfp-hflt.tar.gz that went into /opt/jdk1.8.0_152.

$ locate openhab2.service 
/usr/lib/systemd/system/openhab2.service

The service definition defines the shell script /usr/share/openhab2/runtime/bin/karaf to start.
This script honors JAVA_HOME variable.
So I added it to the openhab2.service file:

Environment=JAVA_HOME=/opt/jdk1.8.0_152

Now reload configuration of systemd

systemctl daemon-reload

and restart OH2

service openhab2 restart

et voila:

ps wwaux | grep [j]ava openhab 754 71.5 1.3 329840 26968 ? Ssl 12:06 0:04 /opt/jdk1.8.0_152/bin/java -Dopenhab.home=/usr/share/openhab2 -Dopenhab.conf=/etc/openhab2 ...

Alternatively you can just define JAVA_HOME in /etc/default/openhab2:

JAVA_HOME=/opt/jdk1.8.0_191
1 Like

sudo update-alternatives --config java

Alston workshops well-known to the switch between versions.