Java
Step-by-Step Guide to Installing JDK and Setting Up Java Environment
By Utility Zone · 2025-11-06T09:10:56.839448
Installing the Java Development Kit (JDK) is essential for developing Java applications. This comprehensive guide covers the installation process and environment setup for Windows, Mac, and Linux operating systems.
Understanding JDK vs JRE vs OpenJDK
Before installation, it's helpful to understand the key differences:123
- JDK (Java Development Kit): Complete development environment including compiler (javac), debugger, and other tools needed for Java development4
- JRE (Java Runtime Environment): Contains only what's needed to run Java applications, not develop them4
- OpenJDK: Free, open-source implementation under GPLv2 license, functionally identical to Oracle JDK25
- Oracle JDK: Commercial product from Oracle with proprietary license requiring paid subscription for production use32
For most developers, OpenJDK is recommended as it's free, open-source, and performs identically to Oracle JDK.2
Installing JDK on Windows
Step 1: Download the JDK
Visit the official Oracle JDK download page at jdk.java.net or download OpenJDK from Adoptium. Select the Windows x64 installer (.exe or .msi file) for your desired Java version (Java 17 or Java 21 are current LTS versions).678
Step 2: Run the Installer
Locate the downloaded .exe or .msi file in your Downloads folder and double-click it to begin installation. Follow the installation wizard prompts. The JDK will be installed to C:\Program Files\Java\jdk-<version> by default.76
Important: The installer now supports only one version per feature release. If you install JDK 21.0.1 after JDK 21, the installer will uninstall JDK 21 automatically.7
Step 3: Set Environment Variables
Set JAVA_HOME
Setting the JAVA_HOME environment variable allows Java-based programs to locate your JDK installation.910
- Search for "Environment Variables" in the Windows taskbar and click "Edit the system environment variables"69
- Click the "Environment Variables" button at the bottom96
- Under System variables, click "New"69
- Enter variable name:
JAVA_HOME96 - Enter variable value:
C:\Program Files\Java\jdk-<YOUR_VERSION>69 - Click OK96
Update the PATH Variable
- In the Environment Variables window, find "Path" under System variables and click "Edit"69
- Click "New" and add:
C:\Program Files\Java\jdk-<YOUR_VERSION>\bin96 - Move this entry to the top of the list if needed9
- Click OK on all windows69
Step 4: Verify Installation
Open a new Command Prompt window and run:116
java -version
javac -version
You should see the installed Java version displayed. If you get "'java' is not recognized as an internal or external command," restart your computer and try again.1213
Installing JDK on Mac
Method 1: Using Homebrew (Recommended)
Homebrew is the easiest way to install and manage Java on Mac.1415
Step 1: Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Step 2: Update Homebrew
brew update
brew tap homebrew/cask-versions
Step 3: Install Java
To install the latest OpenJDK:1415
brew install openjdk
For a specific version like Java 11:15
brew install openjdk@11
Step 4: Set JAVA_HOME
Open your shell configuration file (.bash_profile for bash or .zshrc for zsh):1615
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
For a specific version:15
export JAVA_HOME="/usr/local/opt/openjdk@11"
Save the file and reload it:1615
source ~/.zshrc
Method 2: Manual Installation
Step 1: Download JDK
Visit Oracle's official site or Adoptium and download the .dmg file for macOS.1718
Step 2: Install the Package
Double-click the .dmg file, then double-click the .pkg installer. Follow the installation prompts, entering your password when requested.17
Step 3: Set JAVA_HOME
Open Terminal and edit your shell profile:1016
nano ~/.zshrc
Add these lines:16
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
Reload the configuration:16
source ~/.zshrc
Verify Installation
Open a new Terminal window and run:1916
java -version
javac -version
The output should display your installed Java version.16
Installing JDK on Linux (Ubuntu/Debian)
Step 1: Update Package Index
Open Terminal and update your package list:204
sudo apt update && sudo apt upgrade -y
Step 2: Check Existing Java Installation
Before installing, check if Java is already installed:204
java -version
If Java is not installed, you'll see: Command 'java' not found.420
Step 3: Install OpenJDK
Install Default JDK
To install the default OpenJDK (usually version 17 or 21):204
sudo apt install default-jdk
Install Specific Version
To install a specific OpenJDK version:214
For Java 17:
sudo apt-get install openjdk-17-jdk
For Java 11:
sudo apt-get install openjdk-11-jdk
To see all available versions:21
apt-cache search openjdk
Step 4: Set JAVA_HOME
Open your shell configuration file:2223
nano ~/.bashrc
Add these lines at the end:2322
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Note: Replace java-17-openjdk-amd64 with your actual JDK directory path. To find it, run:24
ls /usr/lib/jvm/
source ~/.bashrc
Alternative: Manual Installation from Archive
For more control over installation location:2526
sudo tar -xzf openjdk-*.tar.gz -C /usr/lib/jvm/
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-<version>/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-<version>/bin/javac 1
Verify Installation
java -version
javac -version
Verify JAVA_HOME:22
echo $JAVA_HOME
Managing Multiple Java Versions
It's common to need different Java versions for different projects.2829
On Windows
You can install multiple JDK versions in separate directories and switch between them by changing the JAVA_HOME environment variable. However, tools like SDKMAN make this process much easier.302928
On Mac
Use the /usr/libexec/java_home utility to switch versions:31
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
Or use jEnv or SDKMAN for easier management.1928
On Linux
Use update-alternatives to manage multiple versions:3227
sudo update-alternatives --config java
Select the version you want from the list.27
Using SDKMAN (Cross-Platform)
SDKMAN is a powerful tool for managing multiple Java versions across all platforms:3328
Install SDKMAN:
curl -s "https://get.sdkman.io" | bash
Install Java:
sdk install java
List available versions:
sdk list java
Switch versions:
sdk default java 17.0.7-jbr
Verification Commands
After installation, verify everything works correctly:343511
# Check Java Runtime Environment
java -version
# Check Java Compiler (JDK)
javac -version
# Check JAVA_HOME
echo $JAVA_HOME # Mac/Linux
echo %JAVA_HOME% # Windows
# Verify PATH
echo $PATH # Mac/Linux
echo %PATH% # Windows
Common Troubleshooting
"java is not recognized as an internal or external command" (Windows)
This error occurs when Java's bin directory is not in your PATH:1312
- Verify JAVA_HOME is set correctly129
- Ensure
%JAVA_HOME%\binis in your PATH variable1312 - Close and reopen Command Prompt13
- Restart your computer if the issue persists12
"javac not recognized" but java works
This means you have the JRE installed but not the JDK:3634
- Install the full JDK (not just JRE)34
- Add the JDK's bin directory to PATH36
- Verify with
javac -version34
Installation Fails with Error 1603
This generic Windows installer error can occur due to:37
- Conflicting previous Java installations
- Insufficient permissions
- Antivirus blocking installation
Solution:37
- Uninstall all existing Java versions3839
- Clean registry using Windows cleanup tools37
- Download the offline installer instead of online version37
- Run installer as Administrator40
JAVA_HOME Points to Wrong Location
If JAVA_HOME is set but programs can't find Java:41
- JAVA_HOME should point to the JDK root directory (e.g.,
C:\Program Files\Java\jdk-21)109 - PATH should point to the bin subdirectory (e.g.,
C:\Program Files\Java\jdk-21\bin)109 - Never point JAVA_HOME to the bin directory or to java.exe41
Uninstalling Java
If you need to completely remove Java:3938
Windows:39
- Open Settings → Apps → Apps & features
- Search for "Java"
- Click Uninstall for each Java version
- Delete remaining folders in
C:\Program Files\Java42
Mac:43
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
sudo rm -fr ~/Library/Application\ Support/Oracle/Java
Linux:4
sudo apt remove openjdk-*
sudo apt autoremove
Best Practices
- Use LTS versions (Java 17 or Java 21) for production applications821
- Keep Java updated for security patches37
- Use OpenJDK unless you specifically need Oracle JDK features442
- Document your Java version in project README files29
- Set JAVA_HOME persistently in shell configuration files2210
- Verify installation after every setup3511
- Use version managers like SDKMAN for easier management of multiple versions2832
By following this guide, you should have a fully functional Java development environment configured on your operating system. Remember to verify your installation with the provided commands and refer to the troubleshooting section if you encounter any issues. <span style="display:none">454647484950515253545556575859606162636465666768697071727374757677</span>
<div align="center">⁂</div>
Footnotes
-
https://www.openlogic.com/blog/java-experts-openjdk-vs-oracle-jdk ↩ ↩2 ↩3 ↩4 ↩5
-
https://www.spinnakersupport.com/blog/2024/05/01/oracle-jdk-vs-openjdk/ ↩ ↩2
-
https://www.linode.com/docs/guides/how-to-install-openjdk-ubuntu-22-04/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9
-
https://www.redhat.com/en/topics/application-modernization/openjdk-vs-oracle-jdk ↩
-
https://www.geeksforgeeks.org/installation-guide/download-and-install-jdk-on-windows-mac-and-linux/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12
-
https://docs.oracle.com/en/java/javase/21/install/installation-jdk-microsoft-windows-platforms.html ↩ ↩2 ↩3
-
https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-install-Java-21 ↩ ↩2
-
https://www.theserverside.com/feature/How-to-set-JAVA_HOME-in-Windows-and-echo-the-result ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14
-
https://docs.oracle.com/en/cloud/saas/enterprise-performance-management-common/diepm/epm_set_java_home_104x6dd63633_106x6dd6441c.html ↩ ↩2 ↩3 ↩4 ↩5
-
https://broadwayinfosys.com/blog/it-career/java-installation-errors/ ↩ ↩2 ↩3 ↩4 ↩5
-
https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-Not-Recognized-Error-Fix ↩ ↩2 ↩3 ↩4
-
https://www.youtube.com/watch?v=mAIxIhVJ7N0 ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
https://snyk.io/blog/install-java-on-macos/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
https://www.geeksforgeeks.org/installation-guide/how-to-install-java-on-macos/ ↩ ↩2
-
https://stackoverflow.com/questions/69875335/macos-how-to-install-java-17 ↩ ↩2
-
https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-do-I-install-Java-on-Ubuntu ↩ ↩2 ↩3 ↩4
-
https://www.hostinger.com/in/tutorials/install-java-ubuntu ↩ ↩2 ↩3
-
https://docs.redhat.com/en/documentation/red_hat_build_of_openjdk/21/html/configuring_red_hat_build_of_openjdk_21_on_rhel/configuring-javahome-environment-variable-on-rhel ↩ ↩2 ↩3 ↩4 ↩5
-
https://docs.oracle.com/en/java/javase/24/install/installation-jdk-linux-platforms.html ↩
-
https://stackoverflow.com/questions/14788345/how-to-install-the-jdk-on-ubuntu-linux ↩ ↩2 ↩3 ↩4
-
https://dev.to/thegroo/install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93 ↩ ↩2 ↩3
-
https://payara.fish/blog/easily-manage-different-java-versions-on-your-machine-with-sdkman/ ↩ ↩2 ↩3 ↩4 ↩5
-
https://stackoverflow.com/questions/70237388/how-to-handle-different-java-versions-for-different-projects-on-the-same-machine ↩ ↩2 ↩3
-
https://support.inductiveautomation.com/hc/en-us/articles/360047170492-Running-multiple-versions-of-Java-on-the-same-Windows-machine ↩
-
https://gist.github.com/tinkerware/8d92524d78f958f3d821b127393a96a1 ↩
-
https://www.baeldung.com/linux/java-choose-default-version ↩ ↩2
-
https://stackoverflow.com/questions/39111269/how-can-i-have-multiple-versions-of-the-java-jdk-installed-on-my-machine-at-the ↩
-
https://stackoverflow.com/questions/22539779/how-to-tell-if-jre-or-jdk-is-installed ↩ ↩2 ↩3 ↩4
-
https://www.reddit.com/r/learnprogramming/comments/dcpcj6/i_have_my_java_path_set_correctly_yet_i_still_get/ ↩ ↩2
-
https://cyfuture.cloud/kb/general/common-java-download-errors-and-how-to-resolve-them ↩ ↩2 ↩3 ↩4 ↩5
-
https://www.java.com/en/download/help/uninstall_java.html ↩ ↩2 ↩3
-
https://www.reddit.com/r/javahelp/comments/apqylu/my_java_installer_wont_install/ ↩
-
https://answers.microsoft.com/en-us/windows/forum/all/issue-with-the-java-path-and-home/6647ee5a-2fdb-43df-9ca9-0ebf72097fe5 ↩ ↩2
-
https://www.java.com/en/uninstall/uninstall_instructions.html ↩
-
https://www.geeksforgeeks.org/installation-guide/how-to-install-jdk-in-linux/ ↩
-
https://docs.oracle.com/en/java/javase/22/install/installation-jdk-linux-platforms.html ↩
-
https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html ↩
-
https://twallet.telangana.gov.in/TABiometric/data/Java Check.pdf ↩
-
https://docs.oracle.com/goldengate/dir1212/gg-director/GDRAD/verifying-jre-environment.htm ↩
-
https://www.geeksforgeeks.org/java/setting-environment-java/ ↩
-
https://stackoverflow.com/questions/18888220/how-to-check-whether-java-is-installed-on-the-computer ↩
-
https://stackoverflow.com/questions/11161248/setting-java-home ↩
-
https://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7 ↩
-
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-20-04 ↩
-
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-22-04 ↩
-
https://stackoverflow.com/questions/52504825/how-to-install-jdk-11-under-ubuntu ↩
-
https://stackoverflow.com/questions/22358071/differences-between-oracle-jdk-and-openjdk ↩
-
https://stackoverflow.com/questions/65601196/how-to-brew-install-java ↩
-
https://www.reddit.com/r/learnprogramming/comments/2nsby9/please_help_setting_up_path_for_java_i_have_tried/ ↩
-
https://www.sololearn.com/en/Discuss/296196/java-path-not-in-cmd ↩
-
https://docs.oracle.com/javacomponents/msi-jre/install-guide/uninstalling-jre-java-removal-and-uninstall-tools.htm ↩
-
https://daisy.github.io/pipeline/Get-Help/Troubleshooting/Common-Errors-Windows/ ↩
-
https://stackoverflow.com/questions/44744015/java-installer-issue-on-windows-10 ↩
-
https://stackoverflow.com/questions/70571066/how-to-delete-java-completely-from-windows ↩
-
https://learn.microsoft.com/en-us/answers/questions/876940/when-i-try-to-install-java-this-error-message-come ↩
-
https://stackoverflow.com/questions/20896848/path-variable-not-working ↩