Module: Introduction to Java

JDK & JRE setup

Java Core: Introduction to Java - JDK & JRE Setup

This document outlines the setup process for the Java Development Kit (JDK) and Java Runtime Environment (JRE). Understanding the difference between these is crucial for Java development.

What are JDK and JRE?

  • JRE (Java Runtime Environment): Provides the minimum requirements to run Java applications. It includes the Java Virtual Machine (JVM), core class libraries, and other supporting files. If you only want to use Java applications, you only need the JRE.

  • JDK (Java Development Kit): A superset of the JRE. It includes everything in the JRE plus development tools needed to create Java applications. This includes the compiler (javac), debugger, and other utilities. If you want to write Java code, you need the JDK.

Think of it this way:

  • JRE: Like a DVD player - it can play pre-made DVDs (Java applications).
  • JDK: Like a DVD player and a DVD authoring studio - it can play DVDs and create them.

1. Downloading the JDK

  1. Choose a Vendor: Several vendors provide JDK distributions. The most common are:

  2. Download the Correct Version: Select the appropriate JDK version for your operating system (Windows, macOS, Linux) and architecture (x64, x86, ARM). Consider the following:

    • LTS (Long-Term Support) Versions: These versions receive updates and security patches for an extended period. Currently, LTS versions are Java 8, Java 11, Java 17, and Java 21. Java 17 or 21 are generally recommended for new projects.
    • Latest Version: Provides the newest features but may have less stability.
  3. Download the Installer: Download the installer file (e.g., .exe for Windows, .dmg for macOS, .tar.gz for Linux).

2. Installing the JDK

The installation process varies slightly depending on your operating system. Here are instructions for common platforms:

A. Windows:

  1. Run the Installer: Double-click the downloaded .exe file.
  2. Follow the Wizard: Accept the license agreement and follow the on-screen instructions.
  3. Important: During installation, the installer will ask for a destination directory. Note this directory! You'll need it later to set the JAVA_HOME environment variable. The default is usually C:\Program Files\Java\jdk-<version>.
  4. Complete Installation: Finish the installation process.

B. macOS:

  1. Open the DMG File: Double-click the downloaded .dmg file.
  2. Run the Installer: Double-click the package file within the DMG.
  3. Follow the Wizard: Accept the license agreement and follow the on-screen instructions.
  4. Important: The JDK is typically installed in /Library/Java/JavaVirtualMachines/jdk-<version>.jdk/Contents/Home. Note this directory!
  5. Complete Installation: Finish the installation process.

C. Linux (Debian/Ubuntu - using tar.gz):

  1. Extract the Archive: Open a terminal and navigate to the directory where you downloaded the tar.gz file. Run the following command:
    tar -xvf jdk-<version>_linux-x64_bin.tar.gz
    
  2. Move the JDK: Move the extracted directory to a suitable location, such as /usr/lib/jvm:
    sudo mv jdk-<version> /usr/lib/jvm
    
  3. Important: Note the full path to the JDK directory (e.g., /usr/lib/jvm/jdk-<version>).

3. Setting the JAVA_HOME Environment Variable

The JAVA_HOME environment variable tells your system where the JDK is installed. This is essential for many Java tools and IDEs to function correctly.

A. Windows:

  1. Search for "Environment Variables": Type "environment variables" in the Windows search bar and select "Edit the system environment variables."
  2. System Properties: Click the "Environment Variables..." button.
  3. System Variables: In the "System variables" section, click "New..."
  4. Variable Name: Enter JAVA_HOME.
  5. Variable Value: Enter the path to your JDK installation directory (e.g., C:\Program Files\Java\jdk-17).
  6. Edit Path Variable: Select the Path variable in the "System variables" section and click "Edit..."
  7. Add New Entry: Click "New" and add %JAVA_HOME%\bin.
  8. OK: Click "OK" on all open windows to save the changes.

B. macOS/Linux:

  1. Open Terminal: Open a terminal window.

  2. Edit Shell Configuration File: Edit your shell configuration file (e.g., .bashrc, .zshrc, .profile). The file you need to edit depends on the shell you're using. For example, if you're using Bash, edit .bashrc.

  3. Add the Following Lines: Add the following lines to the end of the file, replacing <JDK_PATH> with the actual path to your JDK installation directory:

    export JAVA_HOME=<JDK_PATH>
    export PATH=$JAVA_HOME/bin:$PATH
    

    Example (using Java 17 on macOS):

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
    export PATH=$JAVA_HOME/bin:$PATH
    
  4. Source the Configuration File: Run the following command to apply the changes:

    source ~/.bashrc  # Or source ~/.zshrc, source ~/.profile, etc.
    

4. Verifying the Installation

  1. Open a New Terminal/Command Prompt: It's important to open a new terminal or command prompt window to ensure the environment variables are loaded.

  2. Run the Following Command:

    java -version
    javac -version
    
  3. Check the Output: If the installation was successful, you should see output similar to this:

    java version "17.0.2" 2022-01-18
    Java(TM) SE Runtime Environment (build 17.0.2+8-LTS)
    Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS, mixed mode, sharing)
    
    javac 17.0.2
    

    This confirms that both the JRE (java) and the JDK (javac) are installed and accessible.

Troubleshooting

  • java or javac not recognized: Double-check that the JAVA_HOME environment variable is set correctly and that the %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (macOS/Linux) directory is in your PATH.
  • Incorrect Java Version: If you have multiple JDKs installed, ensure that the JAVA_HOME variable points to the desired version.
  • Permissions Issues (Linux): Make sure you have the necessary permissions to execute the JDK files.

This guide provides a comprehensive overview of setting up the JDK and JRE. Remember to choose the appropriate version for your needs and follow the instructions carefully for your operating system. Good luck!