DEV Community

Cover image for JDK Setup On Linux, Windows, Mac
Kwaku Duah
Kwaku Duah

Posted on • Edited on

JDK Setup On Linux, Windows, Mac

πŸš€ 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.

  1. Open System Properties β†’ Advanced β†’ Environment Variables
  2. Under System Variables, edit the Path variable.
  3. Add:
   C:\Program Files\Java\jdk-<version>\bin
Enter fullscreen mode Exit fullscreen mode

βœ… Step 4: Verify Installation

Open Command Prompt and run:

java -version
javac -version
Enter fullscreen mode Exit fullscreen mode

🍎 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
Enter fullscreen mode Exit fullscreen mode

Add these lines:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Reload config:

source ~/.zshrc
# or
source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

βœ… Step 4: Verify Installation

java -version
javac -version
Enter fullscreen mode Exit fullscreen mode

🐧 3. Setting Up JDK on Linux

βœ… Step 1: Install via Terminal

Ubuntu/Debian:

sudo apt update
sudo apt install openjdk-21-jdk
Enter fullscreen mode Exit fullscreen mode

Fedora/CentOS/RHEL:

sudo dnf install java-21-openjdk-devel
Enter fullscreen mode Exit fullscreen mode

Arch Linux:

sudo pacman -S jdk21-openjdk
Enter fullscreen mode Exit fullscreen mode

βœ… Step 2: Configure JAVA_HOME

Edit your shell config file:

nano ~/.bashrc    # or ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Add:

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
export PATH=$JAVA_HOME/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Then reload:

source ~/.bashrc
# or
source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

βœ… Step 3: Verify Installation

java -version
javac -version
Enter fullscreen mode Exit fullscreen mode

πŸ’» 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!");
    }
}
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Step 2: Compile the Program

javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

This creates a HelloWorld.class file.

▢️ Step 3: Run the Program

java HelloWorld
Enter fullscreen mode Exit fullscreen mode

You should see:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

πŸ“š 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)