Java Development Environment Setup
Learning Objectives
By the end of this module, you will be able to: * Understand the core components of the Java platform: JDK, JRE, and JVM. * Install and Configure the correct Java Development Kit (JDK) for your Operating System. * Set up environment variables (JAVA_HOME and PATH) to access Java globally. * Compile and run a Java application from the command line interface (CLI). * Install and configure an IDE (IntelliJ IDEA or VS Code) and Version Control (Git). * Troubleshoot common environment configuration issues.
Learning Progress
- Theoretical understanding of JDK vs JRE vs JVM
- JDK Download and installation
- Environment variable configuration (
JAVA_HOME&PATH) - Installation of IDE & Git
- Compiling & running the first "Hello World" program
Prerequisites and System Requirements
Here is a quick look at the minimal system requirements and platform specifications:
| Component | Windows (10/11) | macOS (Intel / Apple Silicon) | Linux (Ubuntu/Debian/RedHat) |
|---|---|---|---|
| Minimum RAM | 4 GB (8 GB Recommended) | 4 GB (8 GB Recommended) | 4 GB (8 GB Recommended) |
| Disk Space | ~1 GB for JDK + IDE | ~1 GB for JDK + IDE | ~1 GB for JDK + IDE |
| Package Manager | winget (Optional) | Homebrew (Highly Recommended) | apt / yum (Recommended) |
| Preferred JDK | Oracle JDK / Eclipse Temurin | Eclipse Temurin / Azul Zulu (ARM) | OpenJDK |
Folder Structure
This setup module is organized into the following guides:
01_SETUP/
├── README.md # Main entry point and platform overview (This file)
├── Tools.md # Breakdown of standard software tools per OS
├── windows-java-setup.md # Step-by-step setup guide for Microsoft Windows
├── mac-java-setup.md # Step-by-step setup guide for Intel-based macOS
└── apple-silicon-java-setup.md # Optimized setup guide for Apple Silicon M1/M2/M3 Macs
Theory: JDK vs. JRE vs. JVM
To understand what you are installing, you must understand the relationship between the three main building blocks of Java execution:
- JVM (Java Virtual Machine):
- The runtime engine that actually executes Java bytecode.
- It is highly platform-dependent (there are different JVMs for Windows, Mac, and Linux) but allows the bytecode it runs to be completely platform-independent (Write Once, Run Anywhere - WORA).
- JRE (Java Runtime Environment):
- A package that contains the JVM + Core Library Classes + supporting files needed to run compiled Java programs.
- Note: JRE does not contain development tools like a compiler.
- JDK (Java Development Kit):
- The complete software development package.
- It contains the JRE + Development Tools (such as the compiler
javac, the archiverjar, the debuggerjdb, etc.).
Relationship Diagram
Here is how these components nest within each other:
Compilation and Execution Workflow
Java is a hybrid language—it is both compiled and interpreted. The execution flow goes through these steps:
- Developer writes source code in a
.javafile (e.g.,HelloWorld.java). - Compiler (
javac) compiles the source code into platform-independent intermediate code called Bytecode stored in a.classfile. - JVM (
java) loads the bytecode, verifies it, and translates it into native machine code (using an Interpreter + JIT Compiler for performance) for the host operating system to execute.
Step-by-Step Installation Guides
Select the guide below that matches your operating system:
| Platform | Setup Guide Link | Primary Method |
|---|---|---|
| Windows | Windows Java Setup Guide | Executable installer (.exe) + Manual Path Configuration |
| macOS (Intel) | macOS Intel Java Setup Guide | Homebrew Package Manager / Manual DMG Installer |
| Apple Silicon | Apple Silicon M1/M2/M3 Java Setup Guide | Native ARM64 builds via Homebrew / Zulu JDK |
| Tools Breakdown | Recommended Software Tools Catalog | Detailed look at IDEs, Terminals, and Command Utilities |
First Program: Compiling and Running via CLI
Once your environment is set up, let's write and run a simple program to verify everything works.
1. The Code (HelloWorld.java)
Create a file named HelloWorld.java in a clean directory and write:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java World!");
}
}
Code Explanation
public class HelloWorld: Declares a public class namedHelloWorld. In Java, the file name must exactly match the public class name (case-sensitive).public static void main(String[] args): The entry point of any Java application. The JVM looks for this exact signature to start executing code.public: Accessible from anywhere.static: Can be called without creating an instance of the class.void: Does not return any value.main: Name of the method.String[] args: Array of command-line arguments passed to the program.System.out.println(...): Prints the text to the standard output console.
2. Compilation and Execution Commands
Run these commands inside the folder where you saved the file:
Step A: Compile the Java Source Code
What happens? The
javaccompiler checks for syntax errors. If clean, it generates a binary bytecode file namedHelloWorld.classin the same directory.
Step B: Execute the Bytecode via JVM
What happens? The
javacommand launches the JVM and runs the bytecode. Note that we do not add.classextension to the class name here.
Output:
Best Practices
- Use LTS Versions: For learning and production, stick to Long-Term Support (LTS) releases of Java (e.g., Java 17, Java 21) rather than experimental intermediate versions.
- Path Variables: Avoid putting spaces in directory paths where JDK is installed. (e.g.,
C:\Java\jdk-21is safer thanC:\Program Files\Java\jdk-21on some older command-line tools, though modern versions handle spaces better). - Isolate Configurations: If you work on multiple projects that require different Java versions, use a manager like SDKMAN! (on Unix/macOS) to switch JDK versions dynamically without messing up environment variables.
- Capitalization Rules: Remember that Java is highly case-sensitive.
HelloWorldis different fromhelloworld.
Common Mistakes and Troubleshooting
[!WARNING]
1. Error:
javac: command not foundor'javac' is not recognized...
- Cause: The system does not know where
javacis located.- Fix: Double-check your
PATHenvironment variable. Ensure it points to the%JAVA_HOME%\bin(or$JAVA_HOME/binon Unix) folder and that the change has been saved. Remember to restart your terminal window for changes to take effect.[!IMPORTANT]
2. Error:
Could not find or load main class HelloWorld
- Cause: Trying to run
java HelloWorld.classinstead ofjava HelloWorld, or you are running the command from a directory other than where the.classfile is stored.- Fix: Change directory (
cd) to the folder containing the.classfile and runjava HelloWorldwithout any file extension.[!NOTE]
3. Error:
Class names are case-sensitive
- Cause: File name is
helloworld.javabut the public class name inside isHelloWorld.- Fix: Rename the file to match the public class exactly.
Interview Questions (FAQ)
1. What is the difference between JDK, JRE, and JVM?
The difference lies in their scopes: * **JVM** is the abstract machine that executes bytecode. It is the core execution environment. * **JRE** consists of JVM + standard library class files. It runs Java applications. * **JDK** consists of JRE + development tools like the compiler (`javac`). It is used to write and compile Java applications.
2. Why is Java platform-independent, but JVM is platform-dependent?
Java compiler translates Java source code into **bytecode** (which is platform-independent). However, different operating systems (Windows, macOS, Linux) have different underlying instruction sets. Therefore, you need a different, platform-specific **JVM** for each OS to translate that same independent bytecode into the corresponding host operating system's native machine instructions.
3. What is the role of the JIT Compiler?
The **Just-In-Time (JIT) Compiler** is part of the JVM's execution engine. While the JVM interpreter translates bytecode line-by-line (which can be slow), the JIT compiler analyzes running code to find "hot spots" (frequently executed code blocks) and compiles them directly into native machine code. Subsequent calls to these hot spots execute instantly, greatly improving Java application performance.
4. What is the difference between path and classpath?
* **PATH** is an operating system variable used by the shell to find executable files (like `java`, `javac`, `git`). * **CLASSPATH** is a Java environment variable or command parameter used by the JVM to find compiled user-defined classes and packages (`.class` or `.jar` files) during compilation or execution.
Related Topics and Next Steps
Next Module: Let's learn about Java syntax, primitives, variables, and type casting in 02_Introduction