How to Install Java: A Step-by-Step Guide for Beginners

Java is a versatile and widely-used programming language that powers applications ranging from mobile apps to enterprise systems. Before you can write and run Java programs, you need to set up your development environment by installing the Java Development Kit (JDK). This comprehensive guide walks you through the process of installing Java on Windows, macOS, and Ubuntu, providing detailed instructions to ensure a smooth setup. Whether you're a beginner or refreshing your setup, this blog will help you get Java up and running, enabling you to start coding with confidence.

Understanding the Java Installation Components

Before diving into the installation steps, let’s clarify the key components involved in setting up Java. These components are essential for developing and running Java programs, and understanding them will help you navigate the process.

Java Development Kit (JDK)

The JDK is a software development kit that includes everything you need to write, compile, and run Java programs. It contains:

  • Java Compiler (javac): Converts Java source code into bytecode.
  • Java Virtual Machine (JVM): Executes bytecode on your system. Learn more about the JVM.
  • Core Libraries: Provide essential functionalities like data structures and I/O operations.
  • Development Tools: Include utilities for debugging and monitoring.

The JDK is required for developers creating Java applications. You’ll download and install the JDK as the primary step in this guide.

Java Runtime Environment (JRE)

The JRE is a subset of the JDK, designed for running Java applications. It includes the JVM and core libraries but lacks development tools like the compiler. If you only need to run Java programs (e.g., as an end-user), the JRE is sufficient. However, since you’re likely setting up a development environment, the JDK (which includes the JRE) is the right choice.

JVM, JRE, and JDK: What’s the Difference?

To avoid confusion, here’s a quick comparison:

  • JVM: The engine that runs Java bytecode.
  • JRE: JVM + libraries, for running Java apps.
  • JDK: JRE + development tools, for writing and compiling Java code.

For a detailed explanation, check out JVM vs. JRE vs. JDK. For this guide, focus on installing the JDK to cover all your needs.

Choosing the Right JDK Version

Java has multiple versions, with Long-Term Support (LTS) releases like Java 17 and 21 being the most stable for production use. As of June 2025, Java 21 is the latest LTS version, offering modern features like virtual threads and improved performance. For beginners, installing an LTS version ensures compatibility and long-term support.

You can download the JDK from:

  • Oracle: Offers the official JDK with a commercial license for production use.
  • OpenJDK: A free, open-source alternative, widely used for development and production. Distributions like AdoptOpenJDK (now Adoptium) or Amazon Corretto are popular.

For this guide, we’ll use the OpenJDK from Adoptium, as it’s free, reliable, and beginner-friendly. If you prefer Oracle’s JDK, the steps are similar, but check licensing terms for commercial use.

Installing Java on Windows

Windows is a common platform for Java development. Follow these steps to install the JDK on Windows 10 or 11.

Step 1: Download the JDK

  1. Visit the Adoptium website.
  2. Select Temurin 21 (LTS) as the version.
  3. Choose Windows as the operating system and x64 as the architecture (most modern PCs are 64-bit).
  4. Download the .msi installer for ease of use.

Step 2: Run the Installer

  1. Double-click the downloaded .msi file to launch the installer.
  2. Follow the wizard:
    • Accept the license agreement.
    • Choose the installation directory (default is fine, e.g., C:\Program Files\Eclipse Adoptium\jdk-21).
    • Ensure the option to Set JAVA_HOME variable is checked (this simplifies configuration).

3. Click Install and wait for the process to complete. 4. Click Finish to exit the installer.

Step 3: Configure Environment Variables

The JDK requires environment variables to work from the command line:

  • JAVA_HOME: Points to the JDK installation directory.
  • Path: Includes the JDK’s bin folder for accessing tools like javac and java.

If the installer set JAVA_HOME, you may only need to verify it. Otherwise, follow these steps:

  1. Set JAVA_HOME:
    • Right-click This PC > Properties > Advanced system settings > Environment Variables.
    • Under System Variables, click New.
    • Enter JAVA_HOME as the variable name and the JDK path (e.g., C:\Program Files\Eclipse Adoptium\jdk-21) as the value.

2. Update Path:


  • In System Variables, find and select Path, then click Edit.
  • Add a new entry: %JAVA_HOME%\bin.
  • Click OK to save all changes.

Step 4: Verify the Installation

Open a Command Prompt and run:

java -version
javac -version

You should see output like:

openjdk 21.0.1 2023-10-17
OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12)
OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12, mixed mode)
javac 21.0.1

If these commands return version information, your installation is successful. If not, double-check the environment variables or refer to Installation on Windows.

Installing Java on macOS

macOS is popular among developers, and installing Java is straightforward. Here’s how to set up the JDK on macOS.

Step 1: Download the JDK

  1. Go to the Adoptium website.
  2. Select Temurin 21 (LTS).
  3. Choose macOS and x64 (or aarch64 for Apple Silicon Macs).
  4. Download the .pkg installer.

Step 2: Run the Installer

  1. Double-click the downloaded .pkg file.
  2. Follow the installer prompts:
    • Agree to the license.
    • Select the default installation path (e.g., /Library/Java/JavaVirtualMachines/temurin-21.jdk).

3. Complete the installation by entering your admin password if prompted.

Step 3: Configure JAVA_HOME (Optional)

macOS typically doesn’t require manual environment variable setup, but setting JAVA_HOME is useful for development tools: 1. Open Terminal. 2. Edit your shell profile (e.g., ~/.zshrc for zsh or ~/.bashrc for bash):

nano ~/.zshrc
  1. Add the following line:
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
  1. Save and exit, then reload the profile:
source ~/.zshrc

Step 4: Verify the Installation

In Terminal, run:

java -version
javac -version

Expect output similar to the Windows example. If you encounter issues, check the installation path or see Installation on macOS.

Installing Java on Ubuntu

Ubuntu is a popular Linux distribution for developers. These steps apply to Ubuntu 20.04, 22.04, or later.

Step 1: Update Package Lists

Open a Terminal and update the package index:

sudo apt update

Step 2: Install OpenJDK

Adoptium’s OpenJDK is available via Ubuntu’s package manager: 1. Install the JDK:

sudo apt install openjdk-21-jdk
  1. This installs both the JDK and JRE, typically to /usr/lib/jvm/java-21-openjdk-amd64.

Step 3: Configure JAVA_HOME

Set JAVA_HOME for compatibility with development tools: 1. Find the JDK path:

update-java-alternatives -l

Note the path, e.g., /usr/lib/jvm/java-21-openjdk-amd64. 2. Edit your shell profile:

nano ~/.bashrc
  1. Add:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
   export PATH=$JAVA_HOME/bin:$PATH
  1. Save and reload:
source ~/.bashrc

Step 4: Verify the Installation

Run:

java -version
javac -version

You should see version details. If not, verify the JDK path or consult Installation on Ubuntu.

Setting Up a Text Editor or IDE

With the JDK installed, you need a tool to write Java code. You can use:

  • Text Editors: Notepad++, VS Code, or Sublime Text. VS Code with the Java Extension Pack is beginner-friendly.
  • Integrated Development Environments (IDEs): IntelliJ IDEA, Eclipse, or NetBeans. IntelliJ IDEA Community Edition is highly recommended for its features like code completion and debugging.

To set up VS Code: 1. Install VS Code from code.visualstudio.com. 2. Open VS Code, go to Extensions (Ctrl+Shift+X), and install the Extension Pack for Java. 3. Create a new file (e.g., HelloWorld.java) and start coding.

For a simple Java program to test your setup, see Hello Program.

Testing Your Installation with a Sample Program

Let’s confirm your setup by writing and running a simple Java program: 1. Create a file named HelloWorld.java:

public class HelloWorld {
       public static void main(String[] args) {
           System.out.println("Hello, Java!");
       }
   }
  1. Open a terminal in the file’s directory.
  2. Compile the code:
javac HelloWorld.java
  1. Run the program:
java HelloWorld

If you see “Hello, Java!” printed, your installation is working. If you encounter errors, check:

  • File name matches class name (HelloWorld.java).
  • Environment variables are correctly set.
  • JDK is properly installed.

Troubleshooting Common Issues

Here are solutions to common installation problems:

  • “java is not recognized”: Ensure %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Linux/macOS) is in your Path.
  • Version mismatch: If java -version and javac -version show different versions, multiple JDKs may be installed. Use update-java-alternatives (Ubuntu) or reinstall the desired version.
  • Permission errors: Run installers or commands with admin/root privileges (e.g., sudo on Ubuntu).

For platform-specific troubleshooting, refer to Windows, macOS, or Ubuntu guides.

FAQ

Do I need both JDK and JRE?

No, the JDK includes the JRE. Install the JDK for development, as it covers both compiling and running Java programs.

Which Java version should I install?

Choose an LTS version like Java 21 for stability and long-term support. It’s ideal for beginners and production use.

Can I have multiple JDK versions installed?

Yes, but manage them carefully. Use tools like update-java-alternatives (Ubuntu) or set JAVA_HOME to switch versions.

Is OpenJDK as good as Oracle JDK?

For most purposes, OpenJDK is equivalent to Oracle JDK and is free for all uses. Oracle JDK may include additional commercial features.

Do I need an IDE to start coding in Java?

No, a text editor and command-line tools are enough. However, an IDE like IntelliJ IDEA simplifies coding with features like autocompletion.

Conclusion

Installing Java is the first step toward mastering this powerful programming language. By setting up the JDK on Windows, macOS, or Ubuntu, you’ve created a foundation for writing and running Java programs. With your environment ready, explore core concepts like data types or object-oriented programming to build your skills. Keep experimenting, and you’ll soon be crafting robust Java applications!