Getting Started

Java Environment Setup

Learn how to install Java JDK, set up environment variables, and configure your development environment for Java programming.

Prerequisites

Before you start coding in Java, you need to set up your development environment. This involves:

  1. Installing the Java Development Kit (JDK)
  2. Setting up environment variables
  3. Choosing an IDE or text editor
  4. Verifying the installation

JDK vs JRE vs JVM

ComponentFull NamePurpose
JVMJava Virtual MachineExecutes Java bytecode
JREJava Runtime EnvironmentJVM + core libraries (to run Java apps)
JDKJava Development KitJRE + compiler + tools (to develop Java apps)

As a developer, you need the JDK — it includes everything.

Step 1: Download and Install JDK

Windows

  1. Visit Oracle JDK Downloads or use Adoptium (Eclipse Temurin)
  2. Download the Windows installer (.msi or .exe) for the latest LTS version (Java 21)
  3. Run the installer and follow the prompts
  4. Default installation path: C:\Program Files\Java\jdk-21

macOS

bash
# Using Homebrew (recommended)
brew install openjdk@21

# Or download from Oracle/Adoptium website

Linux (Ubuntu/Debian)

bash
# Update package list
sudo apt update

# Install OpenJDK 21
sudo apt install openjdk-21-jdk

# Verify installation
java -version

Step 2: Set Environment Variables

Windows

  1. Open System PropertiesAdvancedEnvironment Variables
  2. Under System Variables, click New:
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-21
  3. 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:$PATH

Then reload:

bash
source ~/.bashrc

Step 3: Verify Installation

Open a terminal or command prompt and run:

bash
java -version

Expected 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 -version
javac 21.0.2

Step 4: Choose an IDE

IDEBest ForCost
IntelliJ IDEAProfessional Java developmentFree Community / Paid Ultimate
EclipseEnterprise Java, plugins ecosystemFree
VS CodeLightweight, multi-languageFree (with Java Extension Pack)
NetBeansBeginners, GUI developmentFree

Recommended: IntelliJ IDEA Community Edition

  1. Download from jetbrains.com/idea
  2. Install and launch
  3. Create a new project → Select JDK 21
  4. Start coding!

Step 5: Write and Run Your First Program

Using Command Line

  1. Create a file named HelloWorld.java:
java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Java is installed and working!");
    }
}
  1. Compile:
bash
javac HelloWorld.java

This creates HelloWorld.class (bytecode).

  1. Run:
bash
java HelloWorld

Output:

Java is installed and working!

Using an IDE

  1. Open your IDE
  2. Create a New Project → Select Java
  3. Create a new Java Class named HelloWorld
  4. Type the code above
  5. Click the Run button (▶)

Common Issues

"javac is not recognized"

  • Make sure JAVA_HOME is set correctly
  • Ensure %JAVA_HOME%\bin is added to your PATH
  • 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 .class extension)
  • Run the command from the directory where the .class file is located

Summary

  • Install the JDK (not just JRE) to develop Java applications
  • Set the JAVA_HOME environment variable and add bin to your PATH
  • Verify installation with java -version and javac -version
  • Choose an IDE like IntelliJ IDEA, Eclipse, or VS Code
  • Compile with javac and run with java from the command line