You are here
Home > java > Core Java >

How to Configure JDK in Eclipse or STS

How to Configure JDK in Eclipse or STS ?

How to Configure JDK in Eclipse or STSConfiguring the Java Development Kit (JDK) in Eclipse or Spring Tool Suite (STS) is a crucial step for any Java developer. Whether you are a beginner or intermediate in java development, you might face some issue in configuring JDK in your IDE such as Eclipse or STS.

This guide provides a detailed step-by-step process for configuring JDK in Eclipse or STS, along with common errors and their solutions.

Prerequisites

Before proceeding with JDK configuration, make sure that you have the following in your system:

  1. JDK Installation: Download and install the latest version of the JDK from Oracle’s official website or OpenJDK if you don’t have it already.
  2. Eclipse or STS Installation: Download and install the latest version of Eclipse IDE or Spring Tool Suite from their respective official websites.

Step-by-Step Guide: How to Configure JDK in Eclipse or STS

Step#1. Verify JDK Installation

  1. Open a terminal or command prompt.
  2. Run the following commands to verify the installation:
    java -version

    Ensure the output displays the installed JDK version correctly.

Step#2. Configure the JDK in Eclipse or STS

Step (A): Open Eclipse/STS Preferences

  1. Launch Eclipse or STS.
  2. Navigate to Window > Preferences (on macOS, go to Eclipse > Preferences).

Step (B): Add the Installed JDK

  1. In the Preferences window, expand Java and select Installed JREs.
  2. Click Add to add a new JDK.
  3. Select Standard VM and click Next.
  4. In the JRE Home field, browse to the JDK installation directory. Add path up to jdk folder. For example:
    • For Windows: C:\Program Files\Java\jdk-21
    • For macOS: /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
    • For Linux: /usr/lib/jvm/java-21-openjdk
  5. In the JRE Name field, add a name of your choice such as ‘jre-21’. We can also name it ‘JDK-21’ for better understanding.
  6. Eclipse will automatically detect the rt.jar or module files and display the list of JDK libraries.
  7. Click Finish.

Step (C): Set the Default JDK

  1. Back in the Installed JREs section, select the newly added JDK.
  2. Click Apply and Close to save the settings.

Step (D): Update Compliance Level

  1. Go to Window > Preferences.
  2. Navigate to Java > Compiler.
  3. In the Compiler compliance level dropdown, select required version eg. 21.
  4. Click Apply and Close.

Step (E): Create a Project

  1. Create a New Project:
    • Go to File > New > Java Project.
    • Enter a project name and click Next.
  2. Configure the JRE:
    • Under JRE, select Use an execution environment JRE and choose JavaSE-21.
  3. Finish Project Setup:
    • Click Finish to create the project.

Step (F): Configure the JDK for Projects

  1. Right-click your project in the Project Explorer.
  2. Select Properties > Java Build Path.
  3. Go to the Libraries tab, click Modulepath or Classpath, and select Add Library.
  4. Choose JRE System Library, click Next, and select Workspace default JRE (JDK 21).
  5. Click Finish, then Apply and Close.

Common Errors and Solutions

  • Error: Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available
    Cause: The JDK path is not configured or incorrectly set in Eclipse.
    Solution: Verify the JDK installation directory and ensure it is correctly added under Window > Preferences > Java > Installed JREs.
  • Error: Version of the JRE/JDK is not compatible
    Cause: The project’s JDK version does not match the configured JDK in Eclipse.
    Solution:

    • Update the JDK version under Window > Preferences > Java > Installed JREs.
    • Ensure the project’s JDK version matches the one set in Project Properties > Java Build Path.
  • Workspace fails to recognize the configured JDK
    Cause: Workspace settings are not updated after adding a new JDK.
    Solution:

    • Restart Eclipse after adding the JDK.
    • Go to Window > Preferences > Java > Installed JREs, select the desired JDK, and click Apply and Close.
  • Error: Compiler compliance level is incompatible
    Cause: The compiler compliance level does not match the configured JDK.
    Solution:

    • Navigate to Window > Preferences > Java > Compiler.
    • Set the Compiler compliance level to match your JDK version.
  • Eclipse does not detect the JDK installed on the system
    Cause: The environment variables ‘JAVA_HOME’ and PATH are not correctly set.
    Solution:

    • Add JAVA_HOME pointing to your JDK installation directory.
    • Update the PATH variable to include the bin directory of the JDK.
  • Error: JDK not visible in Installed JREs
    Cause: The path to the JDK is incorrectly configured.
    Solution:

    • Click Add in Window > Preferences > Java > Installed JREs.
    • Manually browse to the JDK installation directory and add it.
  • Error: Eclipse cannot find tools.jar
    Cause: A JRE is being used instead of a JDK.
    Solution:

    • Replace the JRE with the JDK under Installed JREs.
    • Ensure the project references the correct JDK.
  • Error: Invalid JRE Definition
    Cause: The selected JDK path is incorrect or incomplete.
    Solution:

    • Remove the invalid JDK entry from the Installed JREs.
    • Re-add the JDK with the correct installation path.
  • Error: Class File Version Error
    Cause: The JDK version used to compile the project differs from the runtime JDK.
    Solution: Ensure the Java Compiler settings in Eclipse are set to the desired Java version:

    • Go to Project Properties > Java Compiler.
    • Set the Compiler compliance level to the desired JDK version.
    • Rebuild the project.

You may also go through: How to run your first Java Program in Windows 10+.

Leave a Reply


Top