Java JDK and Environment Setup
Before you can start writing and running Java programs, you need to set up the Java Development Kit (JDK) on your machine. This guide will walk you through the essential components of the Java environment and the steps to get everything running.
Understanding the Trio: JVM, JRE, and JDK
It is important to understand the three core components that make up the Java platform:
1. JVM (Java Virtual Machine)
The JVM is the heart of the “Write Once, Run Anywhere” philosophy. It is an abstract machine that provides the runtime environment in which Java bytecode is executed. It is platform-specific (there are different JVMs for Windows, Mac, and Linux), but it ensures the bytecode remains the same.
2. JRE (Java Runtime Environment)
The JRE is a software layer that runs on top of the operating system. It includes the JVM, core libraries, and other components required to run Java applications. If you only want to use Java apps, you only need the JRE.
3. JDK (Java Development Kit)
The JDK is a full-featured software development kit required to develop Java applications. It includes the JRE plus development tools like the compiler (javac), the archiver (jar), and documentation generators (javadoc).
Rule of Thumb: As a developer, you always install the JDK.
Installing the JDK
We recommend using a modern LTS (Long Term Support) version of Java, such as Java 17 or Java 21.
For Windows
- Download the installer (x64 Installer) from the Oracle Website or use Adoptium.
- Run the
.exefile and follow the installation prompts. - Note the installation path (usually
C:\Program Files\Java\jdk-xx).
For macOS
- Download the DMG installer (Arm64 for Apple Silicon, x64 for Intel).
- Open the
.dmgand run the package installer. - Alternatively, use Homebrew:
Terminal window brew install openjdk@21
For Linux (Ubuntu/Debian)
Use the package manager:
sudo apt updatesudo apt install openjdk-21-jdkConfiguring Environment Variables
To run Java commands from any terminal window, you need to configure your system’s environment variables.
1. Setting JAVA_HOME
This variable points to the directory where the JDK is installed.
- Windows: Search for “Edit the system environment variables” > Environment Variables > New System Variable. Variable name:
JAVA_HOME, Value: path to your JDK. - macOS/Linux: Add the following to your
.zshrcor.bashrc:Terminal window export JAVA_HOME=$(/usr/libexec/java_home)export PATH=$JAVA_HOME/bin:$PATH
2. Updating the PATH
Ensure that the %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Unix) is added to your system’s PATH variable.
Verifying the Installation
Open a new terminal or command prompt and run the following commands:
Check Java Runtime
java -versionExpected output: java version "21.x.x" ...
Check Java Compiler
javac -versionExpected output: javac 21.x.x
Writing Your First “Hello World”
- Create a file named
Main.java. - Paste the following code:
public class Main {public static void main(String[] args) {System.out.println("Hello, Java World!");}}
- Compile the code:
Terminal window javac Main.java - Run the program:
Terminal window java Main
Output: Hello, Java World!
Next Steps
Now that your environment is ready, we will dive into Java Basics: Variables, Data Types, and Operators.