Step-by-Step Guide to Install Java JDK (Windows/Mac/Linux) – Java Course [Chapter 3]
Chapter 3: Installing Java (JDK) – The Ultimate Step-by-Step Guide
Welcome to Chapter 3 of the Java Course. In the previous chapter, we learned the theory behind the JDK, JRE, and JVM. Now, it is time to get our hands dirty.
Installation is often the most frustrating part of learning a new technology. A missing environment variable or a wrong path configuration can lead to the dreaded 'javac' is not recognized error before you even write your first line of code. But do not worry—this guide is designed to be fail-proof.
In this comprehensive tutorial, we will walk through installing the Java Development Kit (JDK) on Windows, macOS, and Linux. We will also cover how to configure your system variables correctly so your computer knows exactly where to find Java.
1. Which Version Should You Choose?
Before we download anything, we need to make a choice. If you visit the official Java website, you will see multiple versions (Java 8, Java 17, Java 21, Java 23...). Which one should you pick?
LTS vs. Non-LTS
Java versions are released every six months, but they fall into two categories:
- LTS (Long Term Support): These versions are supported by Oracle and the community for many years (e.g., 5-8 years). They are stable and used by companies in production.
Examples: Java 8, Java 11, Java 17, Java 21. - Non-LTS (Feature Releases): These are intermediate versions supported only for six months until the next version comes out. They are for testing new features.
Examples: Java 19, Java 20, Java 23.
We will use Java 21 (LTS) for this course. It is the latest major Long Term Support release, meaning it has all the modern features (like Records and Pattern Matching) while being stable enough for professional work.
2. Installing Java on Windows (Step-by-Step)
Since 80% of students use Windows, we will cover this in the most detail. Follow these steps exactly.
Step 1: Download the JDK
- Go to the official Oracle Java Downloads page.
- Scroll down to select Java 21.
- Select the Windows tab.
- Click on the x64 Installer link (usually ends in
.exe, approx 160MB).
Note: If you prefer an open-source version without Oracle's commercial license prompt, you can download "Eclipse Temurin" from Adoptium.net. The steps are identical.
Step 2: Run the Installer
- Double-click the downloaded
.exefile. - A "User Account Control" popup may appear asking for permission. Click Yes.
- The installation wizard will open. Click Next.
- It will ask for the destination folder. The default is usually
C:\Program Files\Java\jdk-21.
Tip: Keep the default path. Changing it to a folder with spaces in the name can sometimes cause issues later. - Click Next and let the installation finish. Click Close when done.
Step 3: Setting Environment Variables (Crucial!)
This is where most beginners get stuck. Even though you installed Java, your Command Prompt (cmd) doesn't know where it is yet. We need to tell Windows where the JDK lives.
A. Copy the Path
- Open your File Explorer.
- Navigate to the installation folder:
C:\Program Files\Java\jdk-21. - Go inside the
binfolder. - Click on the address bar at the top and copy the path. It should look like:
C:\Program Files\Java\jdk-21\bin
B. Open Environment Variables
- Press the Windows Key and search for "env".
- Select "Edit the system environment variables".
- A small window named "System Properties" will open. Click the Environment Variables... button at the bottom right.
C. Update the 'Path' Variable
- In the new window, look at the bottom section labeled System variables (NOT User variables).
- Scroll down to find the variable named Path. Select it and click Edit.
- Click the New button on the right side.
- Paste the path you copied earlier:
C:\Program Files\Java\jdk-21\bin. - Click OK to close the edit window.
D. Set JAVA_HOME (Best Practice)
Many external tools (like Maven, Gradle, or Tomcat) look for a specific variable called JAVA_HOME.
- Back in the "System variables" section, click New....
- Variable name:
JAVA_HOME - Variable value: Paste the path to the JDK folder (without the bin folder).
Example:C:\Program Files\Java\jdk-21 - Click OK on all open windows to save changes.
Step 4: Verify the Installation
- Open a new Command Prompt (Windows Key + R, type
cmd, hit Enter). - Type the following command and press Enter:
If you see output similar to this, Congratulations! You have successfully installed Java.
Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)
3. Installing Java on macOS
Installing on a Mac is generally simpler because macOS handles paths more gracefully, but the new M1/M2 chips require attention.
Step 1: Check Architecture
Click the Apple logo (top left) > About This Mac.
If it says "Intel", you need the x64 installer.
If it says "Apple M1/M2/M3", you need the Arm64 installer.
Step 2: Download and Install
- Go to the Oracle Java Downloads page.
- Select the macOS tab.
- Download the DMG Installer matching your architecture.
- Double-click the file and follow the on-screen instructions (Intro > Read Me > Install).
- You may need to enter your Mac password to authorize the installation.
Step 3: Setting JAVA_HOME (Zsh)
Modern macOS uses the Z-shell (zsh). To make sure development tools find Java:
- Open the Terminal app.
- Open your profile configuration file by typing:
nano ~/.zshrc - Add the following line to the end of the file:
- Press
Ctrl + OthenEnterto save, andCtrl + Xto exit. - Refresh the source:
source ~/.zshrc
4. Installing Java on Linux (Ubuntu/Debian)
For Linux users, we love the terminal. The easiest way is using the package manager (APT).
Method 1: Using APT (Recommended)
Open your terminal (Ctrl + Alt + T) and run the following commands:
sudo apt install openjdk-21-jdk
Type y and press Enter when asked for confirmation. This will install the open-source version of Java 21.
Method 2: Managing Multiple Versions
If you have multiple Java versions installed (e.g., 11 and 21), you can switch between them using:
You will see a list. Type the selection number of the version you want (e.g., 0 or 1) and press Enter.
5. Why did we set 'Environment Variables'?
Beginners often follow the tutorial blindly but don't understand why they are editing confusing system files. Let's demystify this.
The "Path" Variable
When you type a command like javac in the command prompt, Windows doesn't magically know where that program is. It looks in the current folder you are in.
If you are in C:\Users\John, Windows looks for javac.exe inside John's folder. It won't find it.
The Path variable is a list of folders where Windows should look if it can't find the command in the current folder. By adding C:\Program Files\Java\jdk-21\bin to the Path, you are telling Windows: "If you don't see the command here, please check inside the Java bin folder."
The "JAVA_HOME" Variable
This is a convention used by other Java-based tools. For example, if you install Android Studio later, it will check your computer's settings: "Does the user have a variable named JAVA_HOME?" If yes, it uses that path to start running. If you don't set this, many advanced tools will fail to launch.
6. Troubleshooting Common Installation Errors
Here are the most common issues students face during Chapter 3.
Error 1: "'javac' is not recognized..."
Cause: You installed Java, but you didn't add the bin folder to the Path variable.
Fix: Go back to Step 3 of the Windows guide. Make sure you added the path ending in \bin. Also, restart your Command Prompt after making changes. Old CMD windows won't see the new settings.
Error 2: "Error: Could not find or load main class"
Cause: This usually happens later when running code, but sometimes it is due to a "CLASSPATH" variable being set incorrectly in the system.
Fix: Check your Environment Variables. If you see a variable named CLASSPATH in the System Variables, delete it unless you know exactly why you put it there. Java handles the classpath automatically for basic tasks.
Error 3: Installing 32-bit instead of 64-bit
Cause: Accidentally downloading the wrong installer.
Fix: Most modern computers are 64-bit (x64). If you install a 32-bit (x86) JDK, it might limit how much memory your Java programs can use. Always look for "x64" in the filename.
Conclusion
Congratulations! You have successfully crossed the first major hurdle in your programming journey. You have configured your machine to think in Java.
At this point, you have the engine (JDK), but we still need a dashboard to drive the car. Writing code in Notepad is possible, but painful. In the next chapter, we will install an Integrated Development Environment (IDE)—a powerful tool that will write half your code for you.
👉 Next Chapter: Chapter 4: Installing IDE – IntelliJ IDEA / VS Code / Eclipse
(We will set up the best coding tools used by professionals.)
Frequently Asked Questions (FAQ)
1. Can I just install the JRE?
No. As we learned in Chapter 2, the JRE is only for running programs. Since you are a developer who wants to write and compile code, you strictly need the JDK.
2. Should I uninstall older versions of Java?
It is good practice to do so if you don't need them. Having too many versions can sometimes cause conflict if your Path variable is messy. Go to Control Panel > Uninstall a Program and remove older versions like "Java 8 Update 201" if you only plan to use Java 21.
3. Why does the command prompt still show the old version after installing the new one?
This is a "Path priority" issue. In your Environment Variables list, Windows reads from top to bottom. If an older Java path (e.g., C:\Program Files\Java\jdk1.8\bin) is listed above your new Java 21 path, Windows will use the old one. Move your new path to the top of the list.
4. Is OpenJDK safe to use?
Yes, 100%. OpenJDK is the official reference implementation of Java. Major companies like Amazon (Corretto), Microsoft, and RedHat build their own versions based on OpenJDK. For learning purposes, it is identical to Oracle JDK.
5. Do I need to pay for Java?
For personal use, learning, and development, Java is completely free. Oracle JDK is free for development usage. If you are a large company using it in production, you might need a license for Oracle JDK, but you can always use OpenJDK builds for free forever.

Comments
Post a Comment