0
0
Jenkinsdevops~5 mins

JDK configuration in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Jenkins needs Java to run jobs that build and test Java applications. Configuring the JDK in Jenkins tells it where to find Java so it can compile and run your code.
When you want Jenkins to build Java projects using a specific Java version.
When you have multiple Java versions installed and want to choose which one Jenkins uses.
When setting up a new Jenkins server that does not yet know where Java is installed.
When upgrading Java and you want Jenkins to use the new version for builds.
When you want to run Jenkins jobs that require Java commands like javac or java.
Commands
Check the installed Java version on the Jenkins server to confirm Java is available.
Terminal
java -version
Expected OutputExpected
openjdk version "17.0.7" 2023-04-18 OpenJDK Runtime Environment (build 17.0.7+7) OpenJDK 64-Bit Server VM (build 17.0.7+7, mixed mode, sharing)
Use Jenkins CLI with a Groovy script to list configured JDK installations in Jenkins.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 groovy =
Expected OutputExpected
JDK(name=Default, home=/usr/lib/jvm/java-17-openjdk-amd64)
-s - Specifies the Jenkins server URL
Use Jenkins CLI with a Groovy script to add a new JDK configuration pointing to Java 11.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 groovy =
Expected OutputExpected
Added JDK configuration: Java11
-s - Specifies the Jenkins server URL
Verify the new JDK configuration is added by listing all JDKs again.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 groovy =
Expected OutputExpected
JDK(name=Default, home=/usr/lib/jvm/java-17-openjdk-amd64) JDK(name=Java11, home=/usr/lib/jvm/java-11-openjdk-amd64)
-s - Specifies the Jenkins server URL
Key Concept

If you remember nothing else from this pattern, remember: Jenkins needs to know the exact path to your Java installation to build Java projects correctly.

Common Mistakes
Not setting the JAVA_HOME path correctly in Jenkins JDK configuration.
Jenkins cannot find Java commands and build jobs will fail with errors like 'javac not found'.
Set the JAVA_HOME path exactly to the Java installation directory on the Jenkins server.
Assuming Jenkins uses the system Java automatically without configuring JDK in Jenkins settings.
Jenkins may use a different Java version or none at all, causing build inconsistencies.
Always configure the JDK in Jenkins global tool configuration to control which Java version is used.
Summary
Check the installed Java version on the Jenkins server with 'java -version'.
Use Jenkins CLI or the Jenkins UI to add and verify JDK installations.
Set the JAVA_HOME path correctly so Jenkins can find Java to build projects.