Skip to content

Java Environment Setup

Before you start writing Java code, you need to install and configure the Java Development Kit (JDK) on your computer. This chapter will guide you through the entire process.

Understanding JDK, JRE, and JVM

First, let's clarify three core concepts:

  • JVM (Java Virtual Machine): The JVM is an abstract computer and the cornerstone of Java's platform independence. It is responsible for executing compiled Java bytecode. You only need to install the JVM for your specific platform to run any Java program.

  • JRE (Java Runtime Environment): The JRE is the environment needed to run Java programs. It includes the JVM as well as the core class libraries and components necessary for Java program execution. If you only want to run an already compiled Java application, installing the JRE is sufficient.

  • JDK (Java Development Kit): The JDK is the complete toolset for Java developers. It includes everything in the JRE, plus provides the compiler (javac), debugger (jdb), and other development tools. As a developer, you need to install the JDK.

Starting from Java 11, Oracle no longer provides the JRE separately, but packages everything in the JDK. Therefore, our goal is to install the JDK.

Downloading and Installing JDK 21

Several organizations provide JDK builds. The two most common choices are:

  1. Oracle JDK: The official JDK from Oracle. Starting from JDK 17, it is also free for production and commercial use, but please be sure to read its license agreement.
  2. OpenJDK: This is the open-source implementation of Java. We recommend using Eclipse Temurin (from Adoptium), a community-driven, rigorously tested OpenJDK build.

This tutorial will use Eclipse Temurin as an example.

  1. Visit the official Adoptium website: https://adoptium.net/
  2. The website usually automatically detects your operating system. Make sure to select JDK 21 (LTS) version.
  3. Download the appropriate installer for your operating system (Windows, macOS, Linux). For Windows, it's typically a .msi installer; for macOS, a .pkg; for Linux, it can be a .tar.gz archive or installed via package manager.

After downloading, run the installer like regular software and follow the on-screen instructions to complete the installation. It's recommended to keep the default installation path.

Configuring Environment Variables

The installer usually configures most settings automatically, but manually checking and configuring environment variables is good practice, ensuring you can use Java commands in any terminal window.

Windows

  1. Find the JDK installation path: By default, it may be located at C:\Program Files\Eclipse Adoptium\jdk-21.x.x.x.
  2. Set JAVA_HOME:
    • Search for "Edit the system environment variables" in the search bar and open it.
    • In the "System Properties" window, click the "Environment Variables" button.
    • In the "System variables" section, click "New".
    • Enter JAVA_HOME for the variable name.
    • Enter your JDK installation path for the variable value (e.g., C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot).
  3. Configure the Path variable:
    • Find and select Path in "System variables", then click "Edit".
    • In the edit window, click "New", then enter %JAVA_HOME%\bin.
    • Make sure to move this entry to the top of the list to prioritize the version you just installed.

macOS and Linux

For macOS and Linux, environment variables are usually set in the shell configuration file (like ~/.zshrc for Zsh, ~/.bash_profile or ~/.bashrc for Bash).

  1. Find the JDK installation path: On macOS, the installer typically places it in /Library/Java/JavaVirtualMachines/. On Linux, the path depends on your installation method.
  2. Edit the configuration file:
    bash
    # Open the configuration file with your preferred editor, e.g., nano
    nano ~/.zshrc
  3. Add the following lines:
    bash
    # Replace the path with your actual JDK installation path
    export JAVA_HOME="/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home"
    export PATH="$JAVA_HOME/bin:$PATH"
  4. Apply the configuration:
    bash
    source ~/.zshrc

Verifying the Installation

After completing the above steps, open a new terminal or command prompt window and enter the following command to verify that the installation was successful:

bash
# Check Java runtime version
java -version

You should see output similar to this:

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)

Next, check the Java compiler version:

bash
# Check Java compiler version
javac -version

If both commands display version information correctly, congratulations, your Java development environment has been successfully set up!

Content is for learning and research only.