5.0.0~M1-1 openhab.service not picking up java 21 installed using sdkman, but 17 instead

Need some help with this one …
Installed 5.0.0~M1-1 while still on java 17, but after installing java 21, service won’t start.

omr@shs3:~$ java --version
openjdk 21.0.6 2025-01-21 LTS
OpenJDK Runtime Environment Zulu21.40+17-CRaC-CA (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Zulu21.40+17-CRaC-CA (build 21.0.6+7-LTS, mixed mode, sharing)

omr@shs3:~$ echo $JAVA_HOME
/home/omr/.sdkman/candidates/java/current

omr@shs3:~$ apt list -a openhab
Listing... Done
openhab/now 5.0.0~M1-1 all [installed,local]

omr@shs3:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.2 LTS
Release:        24.04
Codename:       noble

omr@shs3:~$ uname -a
Linux shs3 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
omr@shs3:~$

Still, when starting openhab:

omr@shs3:/usr/share/openhab$ sudo systemctl status openhab
[sudo] password for omr:
× openhab.service - openHAB - empowering the smart home
     Loaded: loaded (/usr/lib/systemd/system/openhab.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2025-04-13 15:00:39 CEST; 1h 23min ago
   Duration: 2ms
       Docs: https://www.openhab.org/docs/
             https://community.openhab.org
    Process: 6560 ExecStart=/usr/share/openhab/runtime/bin/karaf ${OPENHAB_STARTMODE} (code=killed, signal=TERM)
    Process: 6561 ExecStop=/usr/share/openhab/runtime/bin/karaf stop (code=exited, status=1/FAILURE)
   Main PID: 6560 (code=killed, signal=TERM)
        CPU: 802ms

Apr 13 15:00:38 shs3 systemd[1]: openhab.service: Scheduled restart job, restart counter is at 20.
Apr 13 15:00:38 shs3 systemd[1]: Started openhab.service - openHAB - empowering the smart home.
Apr 13 15:00:38 shs3 systemd[1]: Stopping openhab.service - openHAB - empowering the smart home...
Apr 13 15:00:39 shs3 karaf[6561]: JVM must be version 21. JVM version 17 is unsupported (JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64)
Apr 13 15:00:39 shs3 systemd[1]: openhab.service: Control process exited, code=exited, status=1/FAILURE
Apr 13 15:00:39 shs3 systemd[1]: openhab.service: Failed with result 'exit-code'.
Apr 13 15:00:39 shs3 systemd[1]: Stopped openhab.service - openHAB - empowering the smart home.
omr@shs3:~$ cat /usr/lib/systemd/system/openhab.service
[Unit]
Description=openHAB - empowering the smart home
Documentation=https://www.openhab.org/docs/
Documentation=https://community.openhab.org
Wants=network-online.target
After=network-online.target

[Service]
Environment=OPENHAB_HOME=/usr/share/openhab
Environment=OPENHAB_CONF=/etc/openhab
Environment=OPENHAB_RUNTIME=/usr/share/openhab/runtime
Environment=OPENHAB_USERDATA=/var/lib/openhab
Environment=OPENHAB_LOGDIR=/var/log/openhab
Environment=OPENHAB_STARTMODE=daemon
EnvironmentFile=-/etc/default/openhab

User=openhab
Group=openhab

WorkingDirectory=/usr/share/openhab
ExecStart=/usr/share/openhab/runtime/bin/karaf ${OPENHAB_STARTMODE}
ExecStop=/usr/share/openhab/runtime/bin/karaf stop

SuccessExitStatus=0 143
RestartSec=5
Restart=on-failure
TimeoutStopSec=120

LimitNOFILE=102642

[Install]
WantedBy=multi-user.target

Nothing set here:

omr@shs3:~$ cat /etc/default/openhab
# openHAB service options

#########################
## PORTS
## The ports openHAB will bind its HTTP/HTTPS web server to.

#OPENHAB_HTTP_PORT=8080
#OPENHAB_HTTPS_PORT=8443

#########################
## HTTP(S) LISTEN ADDRESS
##  The listen address used by the HTTP(S) server.
##  0.0.0.0 (default) allows a connection from any location
##  127.0.0.1 only allows the local machine to connect

#OPENHAB_HTTP_ADDRESS=0.0.0.0

#########################
## BACKUP DIRECTORY
## Set the following variable to specify the backup location.
## runtime/bin/backup and runtime/bin/restore will use this path for the zip files.

#OPENHAB_BACKUPS=/var/lib/openhab/backups

#########################
## JAVA OPTIONS
## Additional options for the JAVA_OPTS environment variable.
## These will be appended to the execution of the openHAB Java runtime in front of all other options.
##
## A couple of independent examples:
##   EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyZWAVE:/dev/ttyUSB0:/dev/ttyS0:/dev/ttyS2:/dev/ttyACM0:/dev/ttyAMA0"
##   EXTRA_JAVA_OPTS="-Djna.library.path=/lib/arm-linux-gnueabihf/ -Duser.timezone=Europe/Berlin -Dgnu.io.rxtx.SerialPorts=/dev/ttyZWave"

EXTRA_JAVA_OPTS="-Duser.timezone=Europe/Oslo"

#########################
## OPENHAB DEFAULTS PATHS
## The following settings override the default apt/rpm locations and should be used with caution.
## openHAB will fail to update itself if you're using different paths.
## Only set these if you are testing and are confident in debugging.

#OPENHAB_HOME=/usr/share/openhab
#OPENHAB_CONF=/etc/openhab
#OPENHAB_RUNTIME=/usr/share/openhab/runtime
#OPENHAB_USERDATA=/var/lib/openhab
#OPENHAB_LOGDIR=/var/log/openhab

#########################
## OPENHAB USER AND GROUP
## The user and group that takes ownership of openHAB. Only available for init.d systems.
## To edit user and group for systemd, see the service file at /usr/lib/systemd/system/openhab.service.

#OPENHAB_USER=openhab
#OPENHAB_GROUP=openhab

#########################
## SYSTEMD START MODE
## The Karaf startmode for the openHAB runtime. Only available for systemctl/systemd systems.
## Defaults to daemon when unset here. Multiple options can be used without quotes.
## debug increases log output. daemon launches the Karaf/openHAB processes.

#OPENHAB_STARTMODE=debug
omr@shs3:~$

How can I get @JAVA_HOME to point to the current sdkman version when starting karaf?
Could it be set by the openhab user in some way I miss?

Maybe try

sudo update-alternatives --config java

and follow prompts. You are showing 21, so may not help but will set version

edit: on second look I see 21 is active, so probably not the solution

Thanks for chiming in …
Finally figured it out curtesy of this post: java - How to use system.d service with SDKMan - Stack Overflow

Needed to add this line to /usr/lib/systemd/system/openhab.service in the [Service] section:

Environment="JAVA_HOME=/home/omr/.sdkman/candidates/java/21.0.6.crac-zulu"

My first time using sdkman so …
It is very login shell-centric so this is how to find what to put in JAVA_HOME for non login shells:

sdk current
java 21.0.6.crac-zulu

sdk use java 21.0.6.crac-zulu

Using java version 21.0.6.crac-zulu in this shell.

echo $JAVA_HOME
/home/omr/.sdkman/candidates/java/21.0.6.crac-zulu

For completeness using sdkman.
Installing a new openHAB version will overwrite the service file.
Create a sticky Drop-In like this:

sudo mkdir /etc/systemd/system/openhab.service.d
sudo nano /etc/systemd/system/openhab.service.d/custom.conf
[Service]
Environment="JAVA_HOME=/home/omr/.sdkman/candidates/java/21.0.6.crac-zulu"
sudo systemctl daemon-reload
sudo systemctl restart openhab

omr@shs3:~$ sudo systemctl status openhab
● openhab.service - openHAB - empowering the smart home
     Loaded: loaded (/usr/lib/systemd/system/openhab.service; enabled; preset: enabled)
    Drop-In: /etc/systemd/system/openhab.service.d
             └─custom.conf
     Active: active (running) since Mon 2025-04-14 19:05:23 CEST; 1min 13s ago
       Docs: https://www.openhab.org/docs/
             https://community.openhab.org
   Main PID: 147819 (java)
      Tasks: 226 (limit: 38106)
     Memory: 1.0G (peak: 1.0G)
        CPU: 4min 42ms
     CGroup: /system.slice/openhab.service
             └─147819 /home/omr/.sdkman/candidates/java/21.0.6.crac-zulu/bin/java -XX:-UsePerfData -Dopenhab.home=/usr/share/openhab -Dopenhab.conf=/etc/openhab -Dopenhab.runtime=/usr/share/openhab/ru>

Apr 14 19:05:23 shs3 systemd[1]: Started openhab.service - openHAB - empowering the smart home.