Unifi controller fails to start – java_home missing exception

Reading Time: 2 minutes

I’m running my local WiFi with Unifi access points. To improve the usability I’ve installed the Unifi WiFi Controller on my Raspberry to manage my WiFi. I figured out that the init script that comes with the Unifi package from their PPA has still problems, even in Version 5.9.29. After a dist-upgrade and a reboot, it often happens that the Unifi service is not starting. If you will check the status by executing systemctl status unifi.service

systemctl status unifi.service

You might get the error, that unifi cannot locate Java Home.

● unifi.service - unifi
   Loaded: loaded (/etc/systemd/system/unifi.service; enabled; vendor preset: enabled)
   Active: activating (start) since Wed 2019-01-23 11:09:41 CET; 953ms ago
  Process: 6732 ExecStop=/usr/lib/unifi/bin/unifi.init stop (code=exited, status=0/SUCCESS)
Cntrl PID: 6756 (unifi.init)
   CGroup: /system.slice/unifi.service
           ├─6756 /bin/bash /usr/lib/unifi/bin/unifi.init start
           └─6783 sleep 1

Jan 23 11:09:41 einsteinium systemd[1]: Starting unifi...
Jan 23 11:09:41 einsteinium unifi.init[6756]: Starting Ubiquiti UniFi Controller: unifiCannot locate Java Home

I took a look whats causing the issue. $JAVA_HOME is set on my system and its delivering the correct Java Home. Finally I figured out that there is a mess in the init-script for Unifi. Here’s how to fix: edit the initscript in /etc/init.d/unifi and hash the whole set_java_home () {….} function. Should look like this

#set_java_home () {
#       arch=`dpkg --print-architecture 2>/dev/null`
#       support_java_ver='8'
#       java_list=''
#       for v in ${support_java_ver}; do
#               java_list=`echo ${java_list} java-$v-openjdk-${arch}`
#               java_list=`echo ${java_list} java-$v-openjdk`
#       done
#
#       # appending Oracle JDK8 (make-jpkg version) to list
#       for a in i386 amd64 armhf arm64; do
#               java_list=$(echo ${java_list} oracle-java8-jdk-${a}/jre)
#       done
#       # appending Oracle JRE8 (make-jpkg version) to list
#       for a in i386 amd64; do
#               java_list=$(echo ${java_list} oracle-java8-jre-${a})
#       done
#       # appending Oracle JDK8 (old make-jpkg version) to list
#       for a in x64 i586 arm32-vfp-hflt arm64-vfp-hflt; do
#               java_list=$(echo ${java_list} jdk-8-oracle-${a}/jre)
#       done
#       # appending Oracle JRE8 (old make-jpkg version) to list
#       for a in x64 i586; do
#               java_list=$(echo ${java_list} jre-8-oracle-${a})
#       done
#
#       # appending Oracle JDK8 (PPA version https://launchpad.net/~webupd8team/+archive/ubuntu/java) to list
#       java_list=$(echo ${java_list} java-8-oracle/jre)
#
#       cur_java=`update-alternatives --query java | awk '/^Value: /{print $2}'`
#       cur_real_java=`readlink -f ${cur_java} 2>/dev/null`
#       for jvm in ${java_list}; do
#               jvm_real_java=`readlink -f /usr/lib/jvm/${jvm}/bin/java 2>/dev/null`
#               [ "${jvm_real_java}" != "" ] || continue
#               if [ "${jvm_real_java}" == "${cur_real_java}" ]; then
#                       JAVA_HOME="/usr/lib/jvm/${jvm}"
#                       return
#               fi
#       done
#
#       alts_java=`update-alternatives --query java | awk '/^Alternative: /{print $2}'`
#       for cur_java in ${alts_java}; do
#               cur_real_java=`readlink -f ${cur_java} 2>/dev/null`
#               for jvm in ${java_list}; do
#                       jvm_real_java=`readlink -f /usr/lib/jvm/${jvm}/bin/java 2>/dev/null`
#                       [ "${jvm_real_java}" != "" ] || continue
#                       if [ "${jvm_real_java}" == "${cur_real_java}" ]; then
#                               JAVA_HOME="/usr/lib/jvm/${jvm}"
#                               return
#                       fi
#               done
#       done
#
#       JAVA_HOME=/usr/lib/jvm/java-8-openjdk-${arch}
#}

Finally, add a new line above or below the hashed block and put your “hardcoded” JAVA_HOME there. For Rasbian (Raspberry Pi 3) it will look like this.

JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf

Save the file, and re-start the service unifi by executing systemctl restart unifi.service