Jenkins: How to Use a Different Java JDK Version in Jenkinsfile
Let’s say that your Jenkins server is running using Java 11, but you need Java 17 to build a project. How can you do that if you don’t use Docker?
Overview
In this tutorial, we will go over the process of downloading and configuring a different JDK that will be used in a Jenkinsfile pipeline configuration. This example assumes that you are not using Docker images in your pipeline, which could be a better, cleaner, and more modern option.
The status quo
Let’s define a simple pipeline in a Jenkinsfile that will help us assert the current Java version that is available. I will be using the declarative pipeline syntax in the examples below.
pipeline {
agent anystages {
stage('Which Java?') {
steps {
sh 'java --version'
}
}
}
}If your Jenkins installation is running on Windows, you need to replace sh with bat.

The JDK Tool Plugin
A typical Jenkins installation already includes the JDK Tool Plugin (now called Oracle Java SE Development Kit Installer). If not, go ahead and install it from the Jenkins plugin manager.
A good indication that the plugin is installed can be found by going to Manage Jenkins > Global Tool Configuration and locating the JDK section.

Installing Java 17 in Jenkins
From the Global Tool Configuration, let’s add a new JDK.
If you are scrolling through the available versions, you will soon notice that you can find anything higher than JDK 9. But we wanted to install JDK 17. What a bummer!

No worries. There is still hope. The first step is to remove this installer configuration from the screen by clicking on the X.

How the configurator should look similar to the screenshot below.

Let’s head to the Java SE 17 Archive Downloads page and locate the appropriate package. Since my Jenkins installation is running on Linux, I will select the Linux x64 Compressed Archive and copy the download link.

Back in the JRE tool, the configuration will look as below:

There are three critical aspects to this configuration that you need to pay close attention to:
- JDK Name — Feel free to name this JDK version as you wish, but you need to reference this name in the Jenkinsfile as well, and it needs to be exactly the same.
- Download URL — Make sure the URL is exactly as on the download page. Ensure you don’t have spaces or other characters before or after the URL value.
- Subdirectory — This needs to be the subdirectory where the JDK will be available after download. In the example given, the directory name is
jdk-17.0.4.1. If the name of the directory is incorrect, the road path will be added to the PATH environment variable.
Configuring the Jenkins pipeline to use a different JDK
We still need to make a few small changes to the Jenkinsfile before we can use the JDK we have just configured.
We need to specify that we want to use the JDK tool in your pipeline using the tools configuration:
tools {
jdk 'Java17'
}Please note that previously I have named the configuration Java17. This needs to be consistent.
The full pipeline will look as follows:
pipeline {
agent any
tools {
jdk 'Java17'
}
stages {
stage('Which Java?') {
steps {
sh 'java --version'
}
}
}
}If you enjoy content like this and it helped you solve a problem, help me create more. Please leave a comment, share, and press that 👏 a few times (up to 50 times). And consider subscribing to Medium for more amazing content.
Rerunning the pipeline and checking the logs should show the following result:

Troubleshooting
The Java version remains unchanged
If you did everything in the tutorial and you still get the same old Java version, the most common culprit is the path to the new JDK version.
Use the following pipeline step in a stage:
echo "$PATH"
This will show in the logs the content of the PATH variable. Here is an example:
/home/valentin/jenkins/tools/hudson.model.JDK/Java17/jdk-17.0.4.1_linux-x64_bin/bin::/home/valentin/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binIf you take the first path and try to access it using the terminal, you should notice that it does not exist, and this is why the new Java version is not being picked up.

Go two levels down and figure out the actual name of the folder where the JDK has been extracted.
WorkflowScript: 5: Tool type “jdk” does not have an install of “jdk17” configured — did you mean “Java17”?
This error indicates a mismatch between the JDK Name configured in the Global Tool Configuration and the name provided to the Jenkinsfile. The names need to be exactly the same. Check for spaces and upper or lower cases.
Conclusion
I hope this tutorial helped you get started using a different Java version in your Jenkins pipeline. Leave a comment in the section below if you have any questions. I would love to hear from you!
Thank you for sticking with this article until the end. If you enjoyed it, please leave a comment, share, and press that 👏 a few times (up to 50 times). It will help others discover this information, and maybe it will help someone else as well.
Follow me on Medium and YouTube if you’re interested in more tutorials like this one.






