Skip to content

Installing Java — JDK Setup, PATH Configuration, and Your First Compilation

DodaTech Updated 2026-06-28 7 min read

In this tutorial, you will learn about Installing Java. We cover key concepts, practical examples, and best practices to help you master this topic.

Installing Java requires downloading a JDK, setting environment variables, and verifying the installation with javac and java commands. Without a proper setup, even a simple "Hello, World!" program will fail with confusing errors like 'javac' is not recognized or java: command not found. This lesson guides you through a clean, professional JDK installation on every major operating system.

What You'll Learn

  • How to choose and download the right JDK distribution
  • How to set JAVA_HOME and update the PATH environment variable
  • How to compile and run a Java program from the command line
  • How the compilation and execution process works

Why It Matters

A correctly configured Java environment is the foundation for everything you will build. Many developers waste hours debugging build tools, IDEs, and frameworks only to discover the root cause is a misconfigured JAVA_HOME or a wrong JDK version.

Real-World Use

Whether you are deploying a Spring Boot microservice to AWS, building an Android app, or running a local Gradle build, the JDK must be properly installed. CI/CD pipelines, Docker containers, and cloud VMs all require the same fundamental setup steps.


Step 1: Choose a JDK Distribution

Since Oracle changed the licensing for Oracle JDK, most developers now use one of the free OpenJDK builds. For this series, we recommend Eclipse Temurin (formerly AdoptOpenJDK).

Distribution Provider License Support
Eclipse Temurin Adoptium (Eclipse Foundation) GPL v2 + CPE Free, LTS available
Amazon Corretto Amazon GPL v2 + CPE Free, LTS available
Azul Zulu Azul Systems GPL v2 + CPE Free, paid options
Oracle OpenJDK Oracle GPL v2 + CPE Free, no LTS guarantee

For this tutorial series, install Eclipse Temurin JDK 21 LTS. Java 21 is the latest LTS release (as of 2026) and includes virtual threads, record patterns, and pattern matching for switch.

  • Eclipse Temurin: https://adoptium.net
  • Amazon Corretto: https://aws.amazon.com/corretto
  • Azul Zulu: https://www.azul.com/downloads

Step 2: Install on Your Operating System

Windows

  1. Download the MSI installer from Adoptium for JDK 21 (x64 or x86-64).
  2. Run the installer. By default, it installs to C:\Program Files\Eclipse Adoptium\jdk-21.0.x.x.
  3. The installer can set JAVA_HOME automatically — check the option during installation.
  4. Verify by opening a new Command Prompt and running:
    java --version
    

If java is not recognized, set environment variables manually:

JAVA_HOME = C:\Program Files\Eclipse Adoptium\jdk-21.0.x.x
PATH append: %JAVA_HOME%\bin

macOS

Using Homebrew (recommended):

brew tap homebrew/cask-versions
brew install --cask temurin21

Or download the .pkg installer from Adoptium and run it. The installer configures JAVA_HOME automatically.

Verify:

java --version
/usr/libexec/java_home -v 21   # prints the JAVA_HOME path

Linux (apt-based, Debian/Ubuntu)

wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo apt-key add -
sudo add-apt-repository --yes https://packages.adoptium.net/artifactory/deb/
sudo apt update
sudo apt install temurin-21-jdk

Verify:

java --version
javac --version

Step 3: Verify the Installation

Run these commands in your terminal:

java --version

Expected output (version numbers may vary):

openjdk 21.0.3 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)
javac --version

Expected output:

javac 21.0.3
echo %JAVA_HOME%   # Windows
echo $JAVA_HOME    # macOS/Linux

Expected: path to your JDK installation directory.

Step 4: Write and Compile Your First Program

Create a file named Hello.java with these exact contents:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Compile it:

javac Hello.java

This produces Hello.class (bytecode). Run it:

java Hello

Expected output:

Hello, Java!

Understanding the Process

Hello.java (source) --[javac]--> Hello.class (bytecode) --[java]--> output

The javac compiler reads your source file and produces a .class file for each class declaration. The java launcher starts the JVM, loads the specified class, and calls its main method.

The main method must always be:

public static void main(String[] args)

The JVM looks for this exact signature. If the class has no main method, you get a MainMethodNotFoundException.

Common Mistakes

  1. Setting JAVA_HOME to the bin directory. JAVA_HOME should be the JDK root (e.g., C:\Program Files\Java\jdk-21), not the bin subfolder.
  2. Adding the JDK root to PATH instead of %JAVA_HOME%\bin. The PATH must include the bin directory where java.exe and javac.exe live.
  3. Running java Hello.class instead of java Hello. The java command takes the class name, not the file name. Do not append .class.
  4. Ignoring case sensitivity on Linux/macOS. The file name must match the public class name exactly: hello.java with public class Hello will fail.
  5. Using the wrong terminal after installation. Environment variable changes take effect only in new terminal windows — do not reuse old ones.

Practice Questions

1. What is the difference between java and javac?
javac is the compiler (source to bytecode). java is the launcher (starts the JVM and loads bytecode).

2. Why does JAVA_HOME need to point to the JDK root and not the bin folder?
Many tools (Maven, Gradle, Tomcat, IntelliJ IDEA) use JAVA_HOME to find JDK libraries and tools. They append \bin themselves.

3. What happens if you run java Hello.class instead of java Hello?
The JVM reports Error: Could not find or load main class Hello.class because it interprets the whole string as a class name.

4. Why does the class name have to match the file name?
The JVM uses the class name to locate the .class file on disk. If they differ, javac produces an error: class Hello is public, should be declared in a file named Hello.java.

5. What is the purpose of the PATH environment variable?
The shell uses PATH to find executables. Adding %JAVA_HOME%\bin lets you run javac and java from any directory.

Challenge Question:
Install two different JDK versions (e.g., 21 LTS and 22). Write a Shell Script or batch file that switches JAVA_HOME between them and prints the active version. Verify that Maven picks up the correct version with mvn --version.

FAQ

What version of Java should I install as a beginner?

Install the latest LTS release — currently Java 21. It provides long-term support from multiple vendors, and most libraries and frameworks target LTS versions. Non-LTS versions (22, 23, etc.) are fine for experimentation but not recommended for production.

Do I need an IDE to write Java code?

No, you can write Java in any text editor and compile with javac. However, an IDE like IntelliJ IDEA Community Edition (free) or VS Code with Java extensions greatly improves productivity with code completion, debugging, and refactoring tools.

What is the difference between OpenJDK and Oracle JDK?

Since Java 11, the source code and features are essentially identical. Oracle JDK includes a few extra commercial tools (like Java Mission Control) but requires a subscription for production use. OpenJDK builds like Temurin are free and functionally equivalent.

What does 'mixed mode, sharing' mean in the java version output?

'Mixed mode' means the JVM can interpret bytecode and also JIT-compile hot methods to native code. 'Sharing' refers to class data sharing (CDS), which improves startup time by pre-loading common classes.

Why does `javac` succeed but `java` says 'could not find or load main class'?

This usually means the classpath is wrong. The JVM looks for the .class file in the current directory by default. If you run java from a different directory, use java -cp . Hello to include the current directory in the classpath.

Mini Project

Create a script verify-java.sh (Linux/macOS) or verify-java.bat (Windows) that:

  1. Prints the current Java version
  2. Prints the JAVA_HOME path
  3. Creates a file App.java with a simple main method
  4. Compiles and runs it
  5. Prints success or explains the error

Test this script on a freshly installed JDK. Also, create a separate script that installs two JDK versions side by side and allows switching between them by setting JAVA_HOME.

What's Next

With Java installed and your first program running, you are ready to understand the anatomy of a Java application deeply. In Lesson 3, you will dissect the Hello World program — every keyword, every brace, and the compilation process step by step. This will prepare you to write more complex programs starting with variables in Lesson 4.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro