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.

Note that using update-alternatives will possibly affect other applications and services on the system. So you might end up in a situation where OH2 runs fine but another service does not start anymore.

When running OH2 as a service on Ubuntu servers, JAVA_HOME can be set specifically for OH2 as follows:

sudo systemctl edit openhab2.service

This will open an empty file. Add the following contents:

[Service]
Environment="JAVA_HOME=/usr/lib/jvm/zulu8"

Adjust your JRE/JDK path accordingly and save the file (CTRL + X, Y if nano is your editor). Then reload the services

systemctl daemon-reload

wait a few seconds and then start or restart OH2:

sudo systemctl start openhab2

This solution is very similar to the one posted by @alex73. However, files in /usr/lib/systemd/system/ should not be modified manually. This location is reserved for vendor-distributed service configurations and should only be modified by package managers like apt.

Instead, user-specific modifications should go to /etc/systemd/system/openhab2.service.d/override.conf. This is where the file automatically ends up when using sudo systemctl edit openhab2.service.