Incorrect java version on Jenkins agent

27 views Asked by At

I have setup a basic jenkins agent as docker container connected with jenkins controller. I want to setup Java a different java version (jdk-21.0.2) on agent when by default has Temurin-17.0.8.1. I setup a different java but when check Java with a simple job script like

pipeline {
    agent { label 'docker-agent1' }
    tools { 
        jdk 'jdk-21.0.2' 
    }
    stages {
        stage('Java test') {
            steps {
                sh '''
                    env | grep -e PATH -e JAVA_HOME
                    which java
                    java -version
                '''
            }
        }
    }
}

It gives me the following output. Notice the output of java -version command. It gives me java version which is by default setup on the agent.

 Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on docker-agent1 in /home/jenkins/workspace/java-test-agent1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
Unpacking https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2_linux-x64_bin.tar.gz to /home/jenkins/tools/hudson.model.JDK/jdk-21.0.2 on docker-agent1
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Java test)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ env
+ grep -e PATH -e JAVA_HOME
LD_LIBRARY_PATH=/opt/java/openjdk/lib/server:/opt/java/openjdk/lib:/opt/java/openjdk/../lib
PATH=/home/jenkins/tools/hudson.model.JDK/jdk-21.0.2/jdk-21.0.2/bin:/home/jenkins/tools/hudson.model.JDK/jdk-21.0.2/jdk-21.0.2/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
JAVA_HOME=/home/jenkins/tools/hudson.model.JDK/jdk-21.0.2/jdk-21.0.2
+ which java
/home/jenkins/tools/hudson.model.JDK/jdk-21.0.2/jdk-21.0.2/bin/java
+ java -version
openjdk version "17.0.8.1" 2023-08-24
OpenJDK Runtime Environment Temurin-17.0.8.1+1 (build 17.0.8.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.8.1+1 (build 17.0.8.1+1, mixed mode)
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Any idea why Java -version is not findding jdk-21.0.2

Additional information: Here is how i have setup jdk-21.0.2 in Manage Jenkins -> Tools

enter image description here

1

There are 1 answers

3
Aditya Malviya On

This will occur if there's another java executable in the PATH that is being picked up before the one from JDK 21.0.2

check path variable and update it or

use Absolute Path, Instead of relying on the PATH environment variable, you can specify the absolute path to the java executable in your Jenkins pipeline script. For example:

groovy

steps {
    sh '''
        /home/jenkins/tools/hudson.model.JDK/jdk-21.0.2/jdk-21.0.2/bin/java -version
    '''
}