Why Environment Setup Matters
Before writing any Java code, you need a properly configured development environment. Unlike interpreted languages like Python, Java requires compilation. Your source code (.java files) must be compiled into bytecode (.class files) before the Java Virtual Machine (JVM) can execute it.
JDK (Java Development Kit)
Contains compiler (javac), JRE, and development tools
Environment Variables
JAVA_HOME and PATH to access Java from anywhere
IDE (Integrated Development Environment)
IntelliJ IDEA, VS Code, or Eclipse for efficient coding
Command Line Tools
javac (compiler) and java (runtime) commands
What We'll Install
Here's everything you need for professional Java development:
JDK 17+ (LTS)
The Java Development Kit includes everything needed to develop and run Java applications. We recommend JDK 17 or 21 (Long Term Support versions).
Includes: javac (compiler), java (runtime), JRE, development tools
IDE (Choose One)
A powerful code editor designed for Java development with features like auto-completion, debugging, and project management.
Options: IntelliJ IDEA (recommended), VS Code, Eclipse
Installing the JDK
We'll install Oracle JDK or OpenJDK. Both work great - OpenJDK is free and open-source, Oracle JDK requires a license for commercial use. For learning, either is fine!
Windows Installation
Download JDK
Visit adoptium.net (Eclipse Temurin - recommended) or oracle.com/java and download JDK 17 or 21 for Windows.
Run the Installer
Double-click the downloaded .msi or .exe file and follow the wizard.
C:\Program Files\Java\jdk-17). You'll need it!
Set JAVA_HOME Environment Variable
Open System Properties (search "Environment Variables" in Start Menu):
- Click "Environment Variables" button
- Under "System variables", click "New"
- Variable name:
JAVA_HOME - Variable value:
C:\Program Files\Java\jdk-17(your JDK path) - Click OK
Add to PATH
Still in Environment Variables:
- Find "Path" under "System variables" and click "Edit"
- Click "New" and add:
%JAVA_HOME%\bin - Click OK on all dialogs
Verify Installation
Open a new Command Prompt (important - must be new!) and run:
java --version
javac --version
You should see version 17.x.x or 21.x.x
macOS Installation
Install via Homebrew (Recommended)
If you have Homebrew installed, this is the easiest way:
brew install openjdk@17
Then create the symlink:
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
Alternative: Download Installer
Download from adoptium.net
and run the .pkg installer.
Set JAVA_HOME (if needed)
Add to your ~/.zshrc or ~/.bash_profile:
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export PATH=$JAVA_HOME/bin:$PATH
Then reload:
source ~/.zshrc
Verify Installation
java --version
javac --version
Linux Installation
Install via Package Manager
Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-17-jdk
Fedora:
sudo dnf install java-17-openjdk-devel
Arch Linux:
sudo pacman -S jdk17-openjdk
Set JAVA_HOME
Add to ~/.bashrc or ~/.zshrc:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
Verify Installation
java --version
javac --version
echo $JAVA_HOME
Setting Up Your IDE
While you can write Java in any text editor, an IDE (Integrated Development Environment) makes you significantly more productive with features like auto-completion, error highlighting, debugging, and refactoring.
IntelliJ IDEA (Recommended)
IntelliJ IDEA is the most popular Java IDE, known for its intelligent code assistance and ergonomic design.
Download IntelliJ IDEA
Visit jetbrains.com/idea and download the Community Edition (free and sufficient for learning).
Install and Launch
Run the installer and follow the setup wizard. On first launch, choose your preferred theme (Darcula recommended).
Create Your First Project
- Click "New Project"
- Select "Java" from the left panel
- Choose your installed JDK from the dropdown (or click "Download JDK")
- Click "Next", then "Next" again (skip templates)
- Name your project (e.g., "HelloJava") and click "Create"
VS Code (Lightweight Alternative)
VS Code is a lightweight, fast editor that works great for Java with the right extensions.
Download VS Code
Visit code.visualstudio.com and download for your OS.
Install Java Extension Pack
Open VS Code, go to Extensions (Ctrl+Shift+X), and search for "Extension Pack for Java" by Microsoft. Install it.
This pack includes: Language Support for Java, Debugger for Java, Maven for Java, and more.
Configure Java Runtime
Press Ctrl+Shift+P, type "Java: Configure Java Runtime" and verify your JDK is detected.
Eclipse (Classic Option)
Eclipse is a veteran IDE with a massive plugin ecosystem, popular in enterprise environments.
Download Eclipse
Visit eclipse.org/downloads and download "Eclipse IDE for Java Developers".
Install and Configure
Extract and run. On first launch, choose a workspace folder for your projects.
Your First Java Program
Let's write the classic "Hello, World!" program to verify everything works. We'll do it both ways - using the command line and using an IDE.
Method 1: Command Line
Create the Source File
Create a file named HelloWorld.java (the filename MUST match the class name):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Welcome to Java programming!");
}
}
Compile the Program
Open terminal/command prompt in the same folder and run:
javac HelloWorld.java
This creates HelloWorld.class (bytecode)
Run the Program
java HelloWorld
Note: Don't include .class extension when running!
Expected Output
Hello, World!
Welcome to Java programming!
Method 2: Using IntelliJ IDEA
Create a New Class
Right-click on src folder → New → Java Class → Name it "HelloWorld"
Write the Code
IntelliJ creates the class skeleton. Add the main method:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
// Let's try some variables too
String name = "Java Developer";
int year = 2026;
System.out.println("Welcome, " + name + "!");
System.out.println("Current year: " + year);
}
}
Tip: Type psvm and press Tab for the main method shortcut!
Run the Program
Click the green play button next to the main method, or press Shift+F10
Understanding the Code
public class HelloWorld - Declares a public class named HelloWorld (must match filename)
public static void main(String[] args) - The entry point of every Java application
System.out.println() - Prints text to the console with a newline
Common Issues & Solutions
Running into problems? Here are the most common issues and how to fix them:
"java" or "javac" is not recognized
Solution: The JDK is not in your PATH. Double-check:
- JAVA_HOME points to your JDK folder (not JRE, not bin folder)
- PATH includes
%JAVA_HOME%\bin(Windows) or$JAVA_HOME/bin(Mac/Linux) - Open a new terminal after changing environment variables
# Verify JAVA_HOME is set correctly
echo %JAVA_HOME% # Windows
echo $JAVA_HOME # Mac/Linux
"Error: Could not find or load main class"
Solution: This usually means:
- You're not in the correct directory (must be where the .class file is)
- The class name doesn't match the filename exactly (case-sensitive!)
- You included .class extension when running (don't do that)
# Correct way to run
java HelloWorld # Good
java HelloWorld.class # Wrong - don't include .class
"class, interface, or enum expected" compiler error
Solution: Check your code structure:
- Make sure all code is inside a class
- Check for missing or extra braces
{ } - Verify all statements end with semicolons
;
IntelliJ says "Project SDK is not defined"
Solution:
- Go to File → Project Structure → Project
- Click the "SDK" dropdown and select your JDK
- If not listed, click "Add SDK" → "JDK" and browse to your JDK installation
VS Code doesn't recognize Java
Solution:
- Make sure "Extension Pack for Java" is installed
- Press Ctrl+Shift+P → "Java: Configure Java Runtime"
- Verify JDK is detected, or manually add the path
- Restart VS Code
Key Takeaways
JDK is Essential
Install JDK 17 or 21 (LTS versions). JDK includes the compiler (javac), runtime (java), and development tools
Configure JAVA_HOME
Set JAVA_HOME environment variable and add the bin folder to PATH for command-line access
Use a Good IDE
IntelliJ IDEA Community Edition is free and provides the best Java development experience
Filename Must Match Class
The Java filename must exactly match the public class name (case-sensitive): HelloWorld.java for class HelloWorld
Compile Then Run
Java is compiled: first use javac to create .class bytecode, then use java to execute it
main() is the Entry Point
Every Java application needs a main method: public static void main(String[] args) - execution starts here
Knowledge Check
Test your understanding of Java environment setup:
What does JDK stand for, and what does it include?
Why do we set the JAVA_HOME environment variable?
Which command compiles a Java source file?
What is the correct signature for the main method in Java?
If your class is named "Calculator", what must the source file be named?
After compiling HelloWorld.java, how do you run the program?