0% found this document useful (0 votes)
21 views5 pages

Install Java 8 Alongside Java 21 and Switch Between Them

This document provides a step-by-step guide for installing Java 8 alongside Java 21 on Ubuntu and switching between them. It includes instructions for checking installed versions, configuring the default Java version system-wide or for specific applications like Jenkins, and setting up a Jenkins pipeline to use Java 8. Additionally, it offers optional methods to lock Java 8 for specific applications and verify the Java version in a Jenkins job.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Install Java 8 Alongside Java 21 and Switch Between Them

This document provides a step-by-step guide for installing Java 8 alongside Java 21 on Ubuntu and switching between them. It includes instructions for checking installed versions, configuring the default Java version system-wide or for specific applications like Jenkins, and setting up a Jenkins pipeline to use Java 8. Additionally, it offers optional methods to lock Java 8 for specific applications and verify the Java version in a Jenkins job.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Install Java 8 alongside Java 21 and switch between them as needed (for Jenkins or your app).

🔧 Step-by-Step: Install Java 8 on Ubuntu (Keep Java 21)


🔹 Step 1: Install Java 8
sudo apt update
sudo apt install openjdk-8-jdk -y

This installs Java 8 (usually in /usr/lib/jvm/java-8-openjdk-amd64).

🔹 Step 2: Check Installed Versions


update-java-alternatives --list

You should see both Java 8 and Java 21 listed.

🔹 Step 3: Configure Default Java Version (Temporarily or Permanently)

Option A: Switch System-Wide Java Version

sudo update-alternatives --config java

Select the number corresponding to Java 8.

Note: This affects the entire system, including Jenkins and other apps.

Option B: Set Java 8 for Jenkins Agent Only (via Environment Variable)

In Jenkins node config:

1. Go to Manage Jenkins → Manage Nodes and Clouds → [Your Node] → Configure.


2. Under Node Properties, check Environment Variables.
3. Add:

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

4. Save.

Now, Jenkins jobs running on that agent will use Java 8, regardless of the system default.
Option C: Use Shell Wrapper in Jenkins Job (Local Scope Only)

In your Jenkins pipeline or freestyle job:

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

java -version

✅ Optional: Lock Java 8 for a Specific Application

If your app is run outside Jenkins, create a wrapper script or systemd service with Java 8 specifically.

readlink -f $(which java)

root@worker-jenkins:/usr/lib/jvm# readlink -f $(which java)

/usr/lib/jvm/java-21-openjdk-amd64/bin/java

root@worker-jenkins:/usr/lib/jvm# ls -al

total 24

drwxr-xr-x 4 root root 4096 May 3 03:50 .

drwxr-xr-x 80 root root 4096 May 3 03:20 ..

-rw-r--r-- 1 root root 1840 Jan 23 22:52 .java-1.21.0-openjdk-amd64.jinfo

-rw-r--r-- 1 root root 2613 Jan 26 07:38 .java-1.8.0-openjdk-amd64.jinfo

lrwxrwxrwx 1 root root 21 Jan 23 22:52 java-1.21.0-openjdk-amd64 -> java-21-openjdk-amd64

lrwxrwxrwx 1 root root 20 Jan 26 07:38 java-1.8.0-openjdk-amd64 -> java-8-openjdk-amd64

drwxr-xr-x 7 root root 4096 May 3 03:20 java-21-openjdk-amd64

drwxr-xr-x 7 root root 4096 May 3 03:50 java-8-openjdk-amd64

/usr/lib/jvm/ java-8-openjdk-amd64/bin/java

JAVA_HOME=/usr/lib/jvm/ java-8-openjdk-amd64/bin/java
pipeline {
agent { label 'maven-build-server' }

stages {
stage('Set Java 8 and Verify') {
steps {
sh '''
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
echo "JAVA_HOME is $JAVA_HOME"
which java
java -version
'''
}
}

stage('Checkout') {
steps {
git branch: 'main',
url: 'https://gitlab.com/learndevopseasy/devsecops/springboot-build-pipeline.git'
}
}

stage('Maven Build') {
steps {
sh '''
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
mvn clean package
'''
}
}
}
}

apt update
apt install -y openjdk-8-jdk

/usr/lib/jvm/ java-8-openjdk-amd64/bin/java

JAVA_HOME=/usr/lib/jvm/ java-8-openjdk-amd64/bin/java

pipeline {
agent { label 'maven-build-server' }

tools {
jdk 'java-8-openjdk' // Must match the name from Global Tool Configuration

stages {
stage('Verify Java') {
steps {
sh '''
echo "Using JAVA_HOME=$JAVA_HOME"
which java
java -version
'''
}
}

}
}
pipeline {
agent { label 'maven-build-server' }

tools {
jdk 'java-8-openjdk' // Must match the name from Global Tool Configuration
maven 'Maven3.8.7'
}

stages {
stage('Verify Java') {
steps {
sh '''
echo "Using JAVA_HOME=$JAVA_HOME"
which java
java -version
mvn --version
'''
}
}

}
}

You might also like