π Setting Up JDK on Windows, macOS, and Linux β Complete Guide
If you're starting out with Java programming, your first essential step is to install the Java Development Kit (JDK). The JDK contains all the tools required to compile, debug, and run Java programs.
In this guide, Iβll walk you through the JDK setup process on Windows, macOS, and Linux, and also show you how to run your first Java program!
πͺ 1. Setting Up JDK on Windows
β Step 1: Download the JDK
Go to either the Oracle JDK or OpenJDK site.
Choose the correct installer (e.g., jdk-21_windows-x64_bin.exe
) for your system.
β Step 2: Install the JDK
- Run the installer and follow the prompts.
- Accept the license agreement and select the default installation path:
C:\Program Files\Java\jdk-<version>
.
β Step 3: Set Environment Variables
To run Java from the command line, add the JDK bin
directory to your PATH
.
- Open System Properties β Advanced β Environment Variables
- Under System Variables, edit the
Path
variable. - Add:
C:\Program Files\Java\jdk-<version>\bin
β Step 4: Verify Installation
Open Command Prompt and run:
java -version
javac -version
π 2. Setting Up JDK on macOS
β Step 1: Download the JDK
Head over to Oracle or Adoptium and download the .dmg
file for macOS.
β Step 2: Install the JDK
Double-click the .dmg
and follow the wizard.
JDK will be installed in /Library/Java/JavaVirtualMachines/
.
β Step 3: Set Environment Variables
Edit your shell config file based on your terminal:
nano ~/.zshrc # For zsh users
nano ~/.bash_profile # For bash users
Add these lines:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
Reload config:
source ~/.zshrc
# or
source ~/.bash_profile
β Step 4: Verify Installation
java -version
javac -version
π§ 3. Setting Up JDK on Linux
β Step 1: Install via Terminal
Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-21-jdk
Fedora/CentOS/RHEL:
sudo dnf install java-21-openjdk-devel
Arch Linux:
sudo pacman -S jdk21-openjdk
β Step 2: Configure JAVA_HOME
Edit your shell config file:
nano ~/.bashrc # or ~/.zshrc
Add:
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
export PATH=$JAVA_HOME/bin:$PATH
Then reload:
source ~/.bashrc
# or
source ~/.zshrc
β Step 3: Verify Installation
java -version
javac -version
π» Writing and Running Your First Java Program
βοΈ Step 1: Create a Java File
Save this as HelloWorld.java
:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
βοΈ Step 2: Compile the Program
javac HelloWorld.java
This creates a HelloWorld.class
file.
βΆοΈ Step 3: Run the Program
java HelloWorld
You should see:
Hello, World!
π Bonus: Intro to Data Structures & Algorithms (DSA) in Java
Now that your JDK is ready, hereβs a glimpse of what you can build next:
π’ Basic Data Structures:
- Arrays: Linear collection of items in memory.
- Linked Lists: Nodes with data + pointer to the next.
- Stacks: LIFO structure.
- Queues: FIFO structure.
- Hash Tables: Key-value mappings.
- Trees: Hierarchical structure (Binary Tree, BST).
- Graphs: Nodes + edges (great for pathfinding, social networks, etc.).
βοΈ Basic Algorithms:
- Sorting: Bubble, Merge, Quick.
- Searching: Linear, Binary.
- Traversal: DFS, BFS (especially useful in trees and graphs).
Mastering DSA is essential for technical interviews and solving real-world coding challenges.
π Conclusion
Setting up the JDK is your gateway into the powerful world of Java. Whether you're building full-stack web apps, preparing for coding interviews, or diving into open-source projectsβthis setup will serve as your foundation.
If you found this guide helpful, consider following me on Dev.to for more tutorials on Python, Java, and full-stack development.
Happy coding! π»β‘
Top comments (0)