Jenkins error "Incorrect Java version" for Java11. After removing Java11 and installing Java8. java is not found anywhere

I've just encountered this problem, and it actually looks like an issue in /etc/init.d/jenkins testing for the Java version. I just posted a solution here: https://dorian.fraser-moore.com/works/5054500/ubuntu-and-jenkins-found-an-incorrect-java-version , sharing below for those who find this question as first google hit.

Following a recent update to my Ubuntu 20.04.1 LTS dev server my installation of Jenkins stopped running on boot. Checking systemctl status jenkins.service returned back this set of messages

Nov 13 09:35:43 tattve jenkins[1038744]: Found an incorrect Java version
Nov 13 09:35:43 tattve jenkins[1038744]: Java version found:
Nov 13 09:35:43 tattve jenkins[1038776]: openjdk version "11.0.9.1" 2020-11-04
Nov 13 09:35:43 tattve jenkins[1038776]: OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
Nov 13 09:35:43 tattve jenkins[1038776]: OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
Nov 13 09:35:43 tattve jenkins[1038744]: Aborting

Which seemed odd, as jenkins support Java 11 and openJDK, indeed I'd been running that before.

Digging around a bit I noticed the init script in /etc/init.d/jenkins that the script, at lines 56-60, was using SED to extract the version from the java - version command into a numeric value, but the regular expression was a bit to broad

# Which Java versions can be used to run Jenkins
JAVA_ALLOWED_VERSIONS=( "18" "110" )
# Work out the JAVA version we are working with:
JAVA_VERSION=$($JAVA -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*".*/\1\2/p;')

It turns out this last line was setting JAVA_VERSION to 11.09 which doesn't match anything in JAVA_ALLOWED_VERSION. I could add 11.09 to JAVA_ALLOWED_VERSIONS to make it run, but a better fix seem to be to fix that permissive sed line. Changing it to:

JAVA_VERSION=$($JAVA -version 2>&1 | sed -n ';s/.* version "\([0-9]*\)\.\([0-9]*\)\..*".*/\1\2/p;')

Did the job. It might help someone else.

[Due Diligence Edit]

It looks like someone has already made a PR for a fix: https://github.com/jenkinsci/packaging/pull/198


Jenkins required java version

Jenkins work perfectly with the Ubuntu provided openjdk packges. You can even decide which OpenJDK version you prefer:

# headless JRE is enough for Jenkins 
$ sudo apt install openjdk-8-jre-headless

# or with JAVA 11
$ sudo apt install openjdk-11-jre-headless

Maintain multible Java version in Ubuntu

The this post on howto maintain multible Java versions.

Install Jenkins (as Debian package from jenkins.io)

There is some very easy documentation on how to install the upstream jenkins Debian packages.

$ wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add
$ sudo bash -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
$ sudo apt update
$ sudo apt-get install jenkins

That should do the trick.

Remove Jenkins

If you are using a packages based Jenkins installation, remove the package with:

$ sudo apt remove jenkins

If you are using the tarball from the Jenkins site, just delete the folder where you installed it.