<p>If your Docker image or Jenkins environment already includes the required Java versions, you can configure Jenkins to use those paths by following these steps:</p>
<p>Steps Eg:
Go to Manage Jenkins &gt; Global Tool Configuration.
Scroll to JDK and configure multiple JDKs:
JDK 8: /usr/lib/jvm/java-8-openjdk-amd64
JDK 11: /usr/lib/jvm/java-11-openjdk-amd64</p>
<p>TO use specific jdk, for freestyle</p>
<p>Enable JDK Selection:
Under the Build Environment section, check the box for &quot;Use a specific JDK&quot;.</p>
<p>For declarative</p>
<p>pipeline {
agent any
tools {
jdk 'JDK11' // Replace 'JDK11' with the name of the JDK in Global Tool Configuration
}
stages {
stage('Build') {
steps {
sh 'java -version' // Command to verify the JDK being used
}
}
}
}</p>

CC BY-SA 4.0
2024-11-20T16:54:29.210Z
2024-11-20T16:54:29.210Z
Bharat goud
<p>I am setting up Jenkins via Docker image jenkins/jenkins:2.462.3-alpine-jdk21. At the moment I have added agent jenkins/inbound-agent:3283.v92c105e0f819-1-alpine3.20-jdk21 and I have a problem that I can't use any other JDK version other than 21. Now it looks like this (when running the pipeline):</p>
<pre><code>/home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java -version
openjdk version &quot;21.0.5&quot; 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode)
</code></pre>
<p>But if I run a similar command in the agent container, it returns what I expect:</p>
<pre><code>/home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java -version
openjdk version &quot;1.8.0_432&quot;
OpenJDK Runtime Environment (Temurin)(build 1.8.0_432-b06)
OpenJDK 64-Bit Server VM (Temurin)(build 25.432-b06, mixed mode)
</code></pre>
<p>Tell me what could be wrong, because the JDK version for alpine is OpenJDK8U-jdk_x64_alpine-linux_hotspot_8u432b06.tar.gz?</p>
<ol>
<li><p>I tried to explicitly set the version via <code>/home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java</code></p>
</li>
<li><p>I tried to specify the version in the stage via</p>
<pre><code>tools {
    jdk 'jdk8'
}
</code></pre>
</li>
<li><p>I tried to set the version via
<code>withEnv([&quot;JAVA_HOME=/var/jenkins_home/tools/hudson.model.JDK/jdk8/jdk8u432-b06&quot;, &quot;PATH=/var/jenkins_home/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin:${env.PATH}&quot;])</code></p>
</li>
<li><p>I tried to set the version via</p>
<pre><code>script {
    def jdkPath = tool name: 'jdk8', type: 'jdk'
    env.JAVA_HOME = jdkPath
    env.PATH = &quot;${jdkPath}/bin:${env.PATH}&quot;
}
</code></pre>
</li>
</ol>
<p>The result is always the same, it uses JDK21.</p>

<jenkins><jenkins-pipeline><continuous-integration><jenkinsfile><java>
Configure the Jenkins agent to use different versions of the JDK
user28374605
# Configure the Jenkins agent to use different versions of the JDK

(Asked by: username_0 on 2024-11-20)

I am setting up Jenkins via Docker image jenkins/jenkins:2.462.3-alpine-jdk21. At the moment I have added agent jenkins/inbound-agent:3283.v92c105e0f819-1-alpine3.20-jdk21 and I have a problem that I can't use any other JDK version other than 21. Now it looks like this (when running the pipeline):
/home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java -version
openjdk version "21.0.5" 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode)

But if I run a similar command in the agent container, it returns what I expect:
/home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java -version
openjdk version "1.8.0_432"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_432-b06)
OpenJDK 64-Bit Server VM (Temurin)(build 25.432-b06, mixed mode)

Tell me what could be wrong, because the JDK version for alpine is OpenJDK8U-jdk_x64_alpine-linux_hotspot_8u432b06.tar.gz?

I tried to explicitly set the version via /home/jenkins/agent/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin/java

I tried to specify the version in the stage via
tools {
    jdk 'jdk8'
}


I tried to set the version via
withEnv(["JAVA_HOME=/var/jenkins_home/tools/hudson.model.JDK/jdk8/jdk8u432-b06", "PATH=/var/jenkins_home/tools/hudson.model.JDK/jdk8/jdk8u432-b06/bin:${env.PATH}"])

I tried to set the version via
script {
    def jdkPath = tool name: 'jdk8', type: 'jdk'
    env.JAVA_HOME = jdkPath
    env.PATH = "${jdkPath}/bin:${env.PATH}"
}



The result is always the same, it uses JDK21.

**Tags:** jenkins, jenkins-pipeline, continuous-integration, jenkinsfile, java


---

## 1 answers

### Answer by username_1 on 2024-11-20. Score: 0

If your Docker image or Jenkins environment already includes the required Java versions, you can configure Jenkins to use those paths by following these steps:

Steps Eg:
Go to Manage Jenkins > Global Tool Configuration.
Scroll to JDK and configure multiple JDKs:
JDK 8: /usr/lib/jvm/java-8-openjdk-amd64
JDK 11: /usr/lib/jvm/java-11-openjdk-amd64

TO use specific jdk, for freestyle

Enable JDK Selection:
Under the Build Environment section, check the box for "Use a specific JDK".

For declarative

pipeline {
agent any
tools {
jdk 'JDK11' // Replace 'JDK11' with the name of the JDK in Global Tool Configuration
}
stages {
stage('Build') {
steps {
sh 'java -version' // Command to verify the JDK being used
}
}
}
}


---

