0
0
JenkinsHow-ToBeginner · 4 min read

How to Configure JDK in Jenkins: Step-by-Step Guide

To configure JDK in Jenkins, go to Manage Jenkins > Global Tool Configuration, then add a new JDK installation by specifying its name and installation path or auto-install options. This setup allows Jenkins jobs to use the configured JDK for building Java projects.
📐

Syntax

In Jenkins, configuring JDK involves defining a JDK installation under the Global Tool Configuration. The key parts are:

  • Name: A label to identify the JDK installation.
  • JAVA_HOME: The path where JDK is installed on the Jenkins server.
  • Automatic Installation: Optionally, Jenkins can download and install JDK automatically.
jenkins
Manage Jenkins > Global Tool Configuration > JDK > Add JDK
- Name: "jdk-17"
- JAVA_HOME: "/usr/lib/jvm/java-17-openjdk"
- Or check "Install automatically" and select JDK version
💻

Example

This example shows how to add JDK 17 manually in Jenkins Global Tool Configuration and use it in a freestyle job.

jenkins
1. Open Jenkins dashboard.
2. Click on "Manage Jenkins".
3. Select "Global Tool Configuration".
4. Scroll to "JDK" section and click "Add JDK".
5. Enter Name: jdk-17
6. Uncheck "Install automatically".
7. Enter JAVA_HOME: /usr/lib/jvm/java-17-openjdk
8. Save the configuration.

To use it in a job:
1. Create a new freestyle project.
2. In "Build Environment", check "Use JDK" and select "jdk-17".
3. Add build steps that require Java.
4. Save and run the job.
Output
Job runs using JDK 17 environment, compiling and running Java code successfully.
⚠️

Common Pitfalls

Common mistakes when configuring JDK in Jenkins include:

  • Not specifying the correct JAVA_HOME path, causing build failures.
  • Forgetting to uncheck "Install automatically" when providing a manual path.
  • Not selecting the configured JDK in the job configuration, so Jenkins uses the default or no JDK.
  • Using incompatible JDK versions for the project requirements.

Always verify the JDK path on the Jenkins server and test the job after configuration.

jenkins
Wrong way:
- Add JDK with incorrect path: /wrong/path/to/jdk
- Job fails with "java not found" error

Right way:
- Add JDK with correct path: /usr/lib/jvm/java-17-openjdk
- Job runs successfully
📊

Quick Reference

StepActionNotes
1Go to Manage Jenkins > Global Tool ConfigurationAccess Jenkins global settings
2Scroll to JDK sectionLocate JDK installations
3Click Add JDKAdd new JDK entry
4Enter Name and JAVA_HOME or enable auto-installName identifies JDK; path or auto-install required
5Save configurationApply changes
6Select JDK in job configurationEnsure job uses configured JDK

Key Takeaways

Configure JDK in Jenkins under Manage Jenkins > Global Tool Configuration > JDK.
Provide a valid JDK name and JAVA_HOME path or enable automatic installation.
Select the configured JDK in your job to ensure it uses the correct Java version.
Verify the JDK path on the Jenkins server to avoid build errors.
Test your job after configuration to confirm the JDK setup works.