Prerequisites
Before you start coding in Java, you need to set up your development environment. This involves:
- Installing the Java Development Kit (JDK)
- Setting up environment variables
- Choosing an IDE or text editor
- Verifying the installation
JDK vs JRE vs JVM
| Component | Full Name | Purpose |
|---|---|---|
| JVM | Java Virtual Machine | Executes Java bytecode |
| JRE | Java Runtime Environment | JVM + core libraries (to run Java apps) |
| JDK | Java Development Kit | JRE + compiler + tools (to develop Java apps) |
As a developer, you need the JDK — it includes everything.
Step 1: Download and Install JDK
Windows
- Visit Oracle JDK Downloads or use Adoptium (Eclipse Temurin)
- Download the Windows installer (
.msior.exe) for the latest LTS version (Java 21) - Run the installer and follow the prompts
- Default installation path:
C:\Program Files\Java\jdk-21
macOS
bash
# Using Homebrew (recommended)
brew install openjdk@21
# Or download from Oracle/Adoptium websiteLinux (Ubuntu/Debian)
bash
# Update package list
sudo apt update
# Install OpenJDK 21
sudo apt install openjdk-21-jdk
# Verify installation
java -versionStep 2: Set Environment Variables
Windows
- Open System Properties → Advanced → Environment Variables
- Under System Variables, click New:
- Variable name:
JAVA_HOME - Variable value:
C:\Program Files\Java\jdk-21
- Variable name:
- Find the Path variable, click Edit, and add:
%JAVA_HOME%\bin
macOS / Linux
Add to your ~/.bashrc, ~/.zshrc, or ~/.bash_profile:
bash
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk
export PATH=$JAVA_HOME/bin:$PATHThen reload:
bash
source ~/.bashrcStep 3: Verify Installation
Open a terminal or command prompt and run:
bash
java -versionExpected output:
openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13)
OpenJDK 64-Bit Server VM (build 21.0.2+13, mixed mode)Also verify the compiler:
bash
javac -versionjavac 21.0.2Step 4: Choose an IDE
| IDE | Best For | Cost |
|---|---|---|
| IntelliJ IDEA | Professional Java development | Free Community / Paid Ultimate |
| Eclipse | Enterprise Java, plugins ecosystem | Free |
| VS Code | Lightweight, multi-language | Free (with Java Extension Pack) |
| NetBeans | Beginners, GUI development | Free |
Recommended: IntelliJ IDEA Community Edition
- Download from jetbrains.com/idea
- Install and launch
- Create a new project → Select JDK 21
- Start coding!
Step 5: Write and Run Your First Program
Using Command Line
- Create a file named
HelloWorld.java:
java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is installed and working!");
}
}- Compile:
bash
javac HelloWorld.javaThis creates HelloWorld.class (bytecode).
- Run:
bash
java HelloWorldOutput:
Java is installed and working!Using an IDE
- Open your IDE
- Create a New Project → Select Java
- Create a new Java Class named
HelloWorld - Type the code above
- Click the Run button (▶)
Common Issues
"javac is not recognized"
- Make sure
JAVA_HOMEis set correctly - Ensure
%JAVA_HOME%\binis added to yourPATH - Restart your terminal after making changes
"Could not find or load main class"
- Make sure your filename matches the class name exactly (case-sensitive)
- Run
java HelloWorld(without.classextension) - Run the command from the directory where the
.classfile is located
Summary
- Install the JDK (not just JRE) to develop Java applications
- Set the
JAVA_HOMEenvironment variable and addbinto your PATH - Verify installation with
java -versionandjavac -version - Choose an IDE like IntelliJ IDEA, Eclipse, or VS Code
- Compile with
javacand run withjavafrom the command line