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

  1. Search for "Environment Variables" in the Windows taskbar and click "Edit the system environment variables"69
  2. Click the "Environment Variables" button at the bottom96
  3. Under System variables, click "New"69
  4. Enter variable name: JAVA_HOME96
  5. Enter variable value: C:\Program Files\Java\jdk-<YOUR_VERSION>69
  6. Click OK96

Update the PATH Variable

  1. In the Environment Variables window, find "Path" under System variables and click "Edit"69
  2. Click "New" and add: C:\Program Files\Java\jdk-<YOUR_VERSION>\bin96
  3. Move this entry to the top of the list if needed9
  4. 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)

Open Terminal and run:1514

/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

Add these lines:1615

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/

Reload the configuration:2422

source ~/.bashrc

Alternative: Manual Installation from Archive

For more control over installation location:2526

  1. Download the .tar.gz archive from OpenJDK1826
  2. Extract the archive:26
sudo tar -xzf openjdk-*.tar.gz -C /usr/lib/jvm/
  1. Set up alternatives:2726
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

Check the installation:244

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

  1. Verify JAVA_HOME is set correctly129
  2. Ensure %JAVA_HOME%\bin is in your PATH variable1312
  3. Close and reopen Command Prompt13
  4. 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

  1. Install the full JDK (not just JRE)34
  2. Add the JDK's bin directory to PATH36
  3. 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

  1. Uninstall all existing Java versions3839
  2. Clean registry using Windows cleanup tools37
  3. Download the offline installer instead of online version37
  4. 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

  1. Open Settings → Apps → Apps & features
  2. Search for "Java"
  3. Click Uninstall for each Java version
  4. 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

  1. Use LTS versions (Java 17 or Java 21) for production applications821
  2. Keep Java updated for security patches37
  3. Use OpenJDK unless you specifically need Oracle JDK features442
  4. Document your Java version in project README files29
  5. Set JAVA_HOME persistently in shell configuration files2210
  6. Verify installation after every setup3511
  7. 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

  1. https://www.youtube.com/watch?v=NzqBXU7-uwY ↩

  2. https://www.openlogic.com/blog/java-experts-openjdk-vs-oracle-jdk ↩ ↩2 ↩3 ↩4 ↩5

  3. https://www.spinnakersupport.com/blog/2024/05/01/oracle-jdk-vs-openjdk/ ↩ ↩2

  4. https://www.linode.com/docs/guides/how-to-install-openjdk-ubuntu-22-04/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9

  5. https://www.redhat.com/en/topics/application-modernization/openjdk-vs-oracle-jdk ↩

  6. 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

  7. https://docs.oracle.com/en/java/javase/21/install/installation-jdk-microsoft-windows-platforms.html ↩ ↩2 ↩3

  8. https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-install-Java-21 ↩ ↩2

  9. 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

  10. https://docs.oracle.com/en/cloud/saas/enterprise-performance-management-common/diepm/epm_set_java_home_104x6dd63633_106x6dd6441c.html ↩ ↩2 ↩3 ↩4 ↩5

  11. https://www.youtube.com/watch?v=O9l5VhXwDtE ↩ ↩2 ↩3

  12. https://broadwayinfosys.com/blog/it-career/java-installation-errors/ ↩ ↩2 ↩3 ↩4 ↩5

  13. https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-Not-Recognized-Error-Fix ↩ ↩2 ↩3 ↩4

  14. https://devqa.io/brew-install-java/ ↩ ↩2 ↩3

  15. https://www.youtube.com/watch?v=mAIxIhVJ7N0 ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8

  16. https://snyk.io/blog/install-java-on-macos/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8

  17. https://www.geeksforgeeks.org/installation-guide/how-to-install-java-on-macos/ ↩ ↩2

  18. https://stackoverflow.com/questions/69875335/macos-how-to-install-java-17 ↩ ↩2

  19. https://www.youtube.com/watch?v=kSAvn73LOR4 ↩ ↩2

  20. https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-do-I-install-Java-on-Ubuntu ↩ ↩2 ↩3 ↩4

  21. https://www.hostinger.com/in/tutorials/install-java-ubuntu ↩ ↩2 ↩3

  22. 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

  23. https://access.redhat.com/solutions/1445833 ↩ ↩2

  24. https://www.youtube.com/watch?v=o8r-LadS4oE ↩ ↩2 ↩3

  25. https://docs.oracle.com/en/java/javase/24/install/installation-jdk-linux-platforms.html ↩

  26. https://stackoverflow.com/questions/14788345/how-to-install-the-jdk-on-ubuntu-linux ↩ ↩2 ↩3 ↩4

  27. https://dev.to/thegroo/install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93 ↩ ↩2 ↩3

  28. https://payara.fish/blog/easily-manage-different-java-versions-on-your-machine-with-sdkman/ ↩ ↩2 ↩3 ↩4 ↩5

  29. https://stackoverflow.com/questions/70237388/how-to-handle-different-java-versions-for-different-projects-on-the-same-machine ↩ ↩2 ↩3

  30. https://support.inductiveautomation.com/hc/en-us/articles/360047170492-Running-multiple-versions-of-Java-on-the-same-Windows-machine ↩

  31. https://gist.github.com/tinkerware/8d92524d78f958f3d821b127393a96a1 ↩

  32. https://www.baeldung.com/linux/java-choose-default-version ↩ ↩2

  33. https://stackoverflow.com/questions/39111269/how-can-i-have-multiple-versions-of-the-java-jdk-installed-on-my-machine-at-the ↩

  34. https://stackoverflow.com/questions/22539779/how-to-tell-if-jre-or-jdk-is-installed ↩ ↩2 ↩3 ↩4

  35. https://www.baeldung.com/java-check-is-installed ↩ ↩2

  36. https://www.reddit.com/r/learnprogramming/comments/dcpcj6/i_have_my_java_path_set_correctly_yet_i_still_get/ ↩ ↩2

  37. https://cyfuture.cloud/kb/general/common-java-download-errors-and-how-to-resolve-them ↩ ↩2 ↩3 ↩4 ↩5

  38. https://www.youtube.com/watch?v=EA608-gzUjM ↩ ↩2

  39. https://www.java.com/en/download/help/uninstall_java.html ↩ ↩2 ↩3

  40. https://www.reddit.com/r/javahelp/comments/apqylu/my_java_installer_wont_install/ ↩

  41. https://answers.microsoft.com/en-us/windows/forum/all/issue-with-the-java-path-and-home/6647ee5a-2fdb-43df-9ca9-0ebf72097fe5 ↩ ↩2

  42. https://www.youtube.com/watch?v=OAX4LzpxRyU ↩

  43. https://www.java.com/en/uninstall/uninstall_instructions.html ↩

  44. https://tuxcare.com/blog/openjdk-vs-oracle-jdk/ ↩

  45. https://www.geeksforgeeks.org/installation-guide/how-to-install-jdk-in-linux/ ↩

  46. https://www.youtube.com/watch?v=lbLDrD26opY ↩

  47. https://docs.oracle.com/en/java/javase/22/install/installation-jdk-linux-platforms.html ↩

  48. https://learn.microsoft.com/en-us/java/openjdk/install ↩

  49. https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html ↩

  50. https://www.youtube.com/watch?v=fuXTyRRge3s ↩

  51. https://www.youtube.com/watch?v=c2HxYFBhuHU ↩

  52. https://learn.microsoft.com/en-us/java/openjdk/download ↩

  53. https://www.java.com/en/download/help/mac_install.html ↩

  54. https://twallet.telangana.gov.in/TABiometric/data/Java Check.pdf ↩

  55. https://docs.oracle.com/goldengate/dir1212/gg-director/GDRAD/verifying-jre-environment.htm ↩

  56. https://www.geeksforgeeks.org/java/setting-environment-java/ ↩

  57. https://stackoverflow.com/questions/18888220/how-to-check-whether-java-is-installed-on-the-computer ↩

  58. https://stackoverflow.com/questions/11161248/setting-java-home ↩

  59. https://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7 ↩

  60. https://www.java.com/en/download/help/version_manual.html ↩

  61. https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-20-04 ↩

  62. https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-22-04 ↩

  63. https://formulae.brew.sh/formula/openjdk ↩

  64. https://stackoverflow.com/questions/52504825/how-to-install-jdk-11-under-ubuntu ↩

  65. https://stackoverflow.com/questions/22358071/differences-between-oracle-jdk-and-openjdk ↩

  66. https://stackoverflow.com/questions/65601196/how-to-brew-install-java ↩

  67. https://openjdk.org/install/ ↩

  68. https://www.reddit.com/r/learnprogramming/comments/2nsby9/please_help_setting_up_path_for_java_i_have_tried/ ↩

  69. https://www.sololearn.com/en/Discuss/296196/java-path-not-in-cmd ↩

  70. https://docs.oracle.com/javacomponents/msi-jre/install-guide/uninstalling-jre-java-removal-and-uninstall-tools.htm ↩

  71. https://daisy.github.io/pipeline/Get-Help/Troubleshooting/Common-Errors-Windows/ ↩

  72. https://www.cs.colostate.edu/helpdocs/JavaPaths.html ↩

  73. https://stackoverflow.com/questions/44744015/java-installer-issue-on-windows-10 ↩

  74. https://www.java.com/en/download/help/path.html ↩

  75. https://stackoverflow.com/questions/70571066/how-to-delete-java-completely-from-windows ↩

  76. https://learn.microsoft.com/en-us/answers/questions/876940/when-i-try-to-install-java-this-error-message-come ↩

  77. https://stackoverflow.com/questions/20896848/path-variable-not-working ↩