ImageJ doesn’t start from icon click on Ubuntu

A bug in Ubuntu 16.04 stops ImageJ from starting from the icon. While it’s better to install ImageJ directly, here’s how to fix Ubuntu’s system ImageJ version if it won’t start when clicking on the ImageJ icon. The issue is that it isn’t finding JAVA_HOME as confirmed by typing in Terminal

imagej
  1. Edit the imagej startup script

     sudo nano /usr/bin/imagej
    
  2. update the line to say

     JAVA_HOME=$(/usr/sbin/update-java-alternatives -l | grep openjdk | sort | tail -1 | tr -s ' ' | cut -d' ' -f 3)
    

    notice the added tr -s ' ' to squash repeated spaces, which is what breaks cut.

Other alternatives include

JAVA_HOME=/usr/lib/jvm/default-java