Installing Java — JDK Setup, PATH Configuration, and Your First Compilation
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_HOMEand update thePATHenvironment 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.
Download Links
- 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
- Download the MSI installer from Adoptium for JDK 21 (x64 or x86-64).
- Run the installer. By default, it installs to
C:\Program Files\Eclipse Adoptium\jdk-21.0.x.x. - The installer can set
JAVA_HOMEautomatically — check the option during installation. - 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
- Setting
JAVA_HOMEto thebindirectory.JAVA_HOMEshould be the JDK root (e.g.,C:\Program Files\Java\jdk-21), not thebinsubfolder. - Adding the JDK root to PATH instead of
%JAVA_HOME%\bin. ThePATHmust include thebindirectory wherejava.exeandjavac.exelive. - Running
java Hello.classinstead ofjava Hello. Thejavacommand takes the class name, not the file name. Do not append.class. - Ignoring case sensitivity on Linux/macOS. The file name must match the public class name exactly:
hello.javawithpublic class Hellowill fail. - 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
Mini Project
Create a script verify-java.sh (Linux/macOS) or verify-java.bat (Windows) that:
- Prints the current Java version
- Prints the
JAVA_HOMEpath - Creates a file
App.javawith a simple main method - Compiles and runs it
- 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