JDK Installation Guide
Installing JDK on Windows
System Requirements
Before installing JDK, ensure your computer meets the following minimum requirements: For processor, Intel Core i3 or equivalent is recommended; For memory, at least 4GB RAM is recommended, 8GB or more is preferred; For disk space, at least 2GB free space for JDK installation, with additional space for development projects; For operating system, Windows 10 or Windows 11 64-bit versions.
While Windows 7 and 8.1 might work, Oracle no longer officially supports these operating systems. If running on older systems, compatibility issues may occur.
Downloading JDK
First, visit the Oracle official website at https://www.oracle.com/java/technologies/downloads/ to download JDK. On the download page, you can see different JDK versions: JDK 25 is the newest version, JDK 21 is the latest Long-Term Support version, and JDK 17 is also a mature LTS version. Choose the appropriate version based on your project requirements.
For Windows systems, choose either x64 Installer (.exe) or x64 MSI Installer (.msi). The .exe is the traditional installation wizard, while .msi is suitable for batch deployment in enterprise environments. If you only need to run Java programs without installation, choose x64 Compressed Archive (.zip), which can be extracted and used directly.
Pay attention to the license terms during download. Oracle JDK is free under certain versions and usages but has restrictions on commercial use and redistribution. If you need completely free usage, consider using OpenJDK or other OpenJDK-based distributions like Eclipse Temurin.
Installation Steps
Find the downloaded JDK installation file (usually jdk-xx_windows-x64_bin.exe) and double-click to run. In the installation wizard, click "Next" to continue. First, you'll see the target folder selection screen. The default installation path is C:\Program Files\Java\jdk-xx. If you need to change the installation location, click "Change" to choose another directory. It's recommended to use the default path to avoid subsequent configuration issues.
Next is the feature selection screen. By default, all features are selected, including development tools, source code, and public JRE. It's recommended to keep the default full installation. Click "Next" to start the installation. The installation process may take a few minutes; please be patient.
After installation completes, you'll see a prompt. Click "Close" to finish. At this point, JDK is installed in the specified directory, but environment variables need to be configured to use java, javac, and other commands from the command line.
Configuring Environment Variables
The purpose of configuring environment variables is to let the system find the JDK installation location so you can use java, javac, and other commands from any directory. Right-click "This PC" or "My Computer" and select "Properties." In the left menu, click "Advanced system settings."
In the opened System Properties window, click the "Environment Variables" button. In the "System variables" section, click "New." Enter JAVA_HOME for the variable name and the JDK installation path (such as C:\Program Files\Java\jdk-21) for the variable value, then click OK.
Find the Path variable in system variables and click "Edit." Click "New" and enter %JAVA_HOME%\bin. Make sure this line is at the top so the newly installed JDK takes priority. Click OK to save all settings.
Open a new command line window (cmd or PowerShell) and enter java -version and javac -version. If you see version information, the configuration is successful.
Verifying Installation
Open Command Prompt (Win+R, enter cmd), and run the following commands to verify JDK is correctly installed:
Run java -version, which should display the Java Runtime Environment version information. Run javac -version, which should display the Java compiler version information. Run echo %JAVA_HOME%, which should display the previously set JAVA_HOME path.
If any command shows an error, check whether environment variables are configured correctly or whether JDK is properly installed.
Installing JDK on macOS
Using DMG Installation Package
macOS users can quickly install JDK using the DMG installation package. Visit the Oracle official download page and download the macOS version of JDK. For Apple Silicon (M1/M2/M3 chips), choose ARM64 DMG Installer; for Intel chip Macs, choose x64 DMG Installer.
After downloading, open the DMG file, and you'll see a pkg installation file. Double-click to run the installation wizard and follow the prompts to complete the installation. The installer will place JDK in the /Library/Java/JavaVirtualMachines/ directory.
After installation completes, open Terminal and type java -version to verify. If there are multiple JDK versions on the system, use /usr/libexec/java_home -V to view all installed versions.
Using Homebrew
If Homebrew package manager is already installed, you can use it to install JDK, which is more convenient. Open Terminal and run: brew update. Then install JDK 21: brew install openjdk@21.
After installation completes, you need to add JDK to PATH. Run the following command to link JDK to the system: sudo ln -sfn $(brew --prefix)/opt/openjdk@21/libexec/openjdk /Library/Java/JavaVirtualMachines/openjdk-21.
Then add environment variables in ~/.zshrc or ~/.bash_profile: export PATH="/usr/local/opt/openjdk@21/bin:$PATH". Save and run source ~/.zshrc to apply the configuration.
Configuring JAVA_HOME
On macOS, configuring JAVA_HOME is not mandatory because the system automatically finds installed JDKs. However, some tools (like Maven, Gradle) may require manual configuration. In Terminal, edit the ~/.zshrc file and add: export JAVA_HOME=$(/usr/libexec/java_home). Save and run source ~/.zshrc to apply the configuration.
Installing JDK on Linux
Using APT (Debian/Ubuntu)
On Debian and Ubuntu systems, you can install OpenJDK using the APT package manager. First, update the package list: sudo apt update. Then install OpenJDK 21 development version: sudo apt install openjdk-21-jdk. If you only need to run Java programs without the compiler, install JRE: sudo apt install openjdk-21-jre.
During installation, you'll be prompted to create the JAVA_HOME directory. Select "Yes" for automatic configuration. After installation completes, use java -version and javac -version to verify the installation.
If you need to manually configure JAVA_HOME, edit the ~/.bashrc or ~/.profile file and add: export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64. Then run source ~/.bashrc to apply the configuration.
Using YUM (Red Hat/CentOS)
On systems using YUM like Red Hat, CentOS, and Fedora, you can directly install JDK. Run the following command to install OpenJDK 21: sudo yum install java-21-openjdk java-21-openjdk-devel. The -devel package includes the compiler and other development tools.
After installation completes, use the alternatives command to set the default Java version: sudo alternatives --config java. Follow the prompts to select the Java version to set as default.
Edit the ~/.bashrc file and add: export JAVA_HOME=/usr/lib/jvm/java-21-openjdk. Run source ~/.bashrc to apply the configuration.
Manually Installing Binary Files
For situations requiring installing a specific JDK version, you can manually download and extract binary archive files. Visit Oracle or OpenJDK download page and download the .tar.gz file for the desired version.
Create the JDK installation directory: sudo mkdir -p /opt/jdk. Extract the downloaded file: sudo tar -xzf jdk-21_linux-x64_bin.tar.gz -C /opt/jdk. Enter the extracted directory and note the full path.
Edit the ~/.bashrc file and add: export JAVA_HOME=/opt/jdk/jdk-21 and export PATH=$JAVA_HOME/bin:$PATH. Run source ~/.bashrc to apply the configuration.
Managing Multiple JDK Versions
Windows Multi-Version Management
After installing multiple JDK versions on Windows, switch the default version through environment variables. The simplest method is to modify the order of JDK bin directories in the PATH environment variable, placing the version you want to use at the front.
Another method is to use the JAVA_HOME environment variable, pointing it to different JDK installation directories. This works for most tools, but some tools may directly use the java command in PATH rather than JAVA_HOME.
Some third-party tools can help manage multiple JDK versions, like SDKMAN! for Windows or JetBrains Toolbox. These tools make switching between versions more convenient.
macOS Multi-Version Management
On macOS, you can use the jenv tool to manage multiple JDK versions. First, install jenv: brew install jenv. Then add jenv initialization to your shell configuration file.
Add installed JDK to jenv: jenv add /Library/Java/JavaVirtualMachines/jdk-21/Contents/Home. Then use jenv versions to view all managed versions and jenv global xx to set the global default version.
Another method is to use /usr/libexec/java_home -v to specify a particular version to run programs. For example: /usr/libexec/java_home -v 21 --exec java -version.
Linux Multi-Version Management
On Linux, use the update-alternatives tool to manage multiple JDK versions. First, create alternatives entries for each JDK version: sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk-21/bin/java 1.
Then use update-alternatives --config java to select the default version. jenv is also available on Linux, with installation and configuration methods similar to macOS.
Common Installation Issues
Cannot Find java Command After Installation
If you cannot find the java command after installing JDK, first confirm whether JDK is installed correctly. Check if JAVA_HOME environment variable is set correctly and whether PATH includes %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Linux/macOS).
On Windows, open a new command line window because environment variable changes may not take effect immediately. On Linux/macOS, run source ~/.bashrc or reopen the terminal.
If the problem persists, check whether the JDK installation directory is correct and whether the bin/java file exists in that directory.
Permission Issues
When installing JDK on Linux and macOS, you may encounter permission issues. When using package managers, administrator privileges (sudo) are usually required. When manually installing, ensure the installation directory has appropriate read and write permissions.
If regular users cannot use the java command, check permissions for the JAVA_HOME directory and JDK bin directory. You can use ls -la /usr/lib/jvm to check directory permissions.
JAVA_HOME Setting Not Working
If setting JAVA_HOME doesn't work, check whether the environment variable syntax is correct. On Windows, the variable value should not contain quotes. On Linux/macOS, ensure you're using the correct export command.
Also check whether other scripts or configuration files are overriding the JAVA_HOME setting. On Linux, system-level configuration may be in /etc/environment or /etc/profile.d/.
Uninstalling Old Versions
After upgrading JDK, you may need to uninstall old versions to save disk space. On Windows, find the old JDK version in Control Panel's "Programs and Features" and click Uninstall. On macOS, delete the old version folder in /Library/Java/JavaVirtualMachines/. On Linux, use the package manager to uninstall: sudo apt remove openjdk-xx-jdk (APT) or sudo yum remove java-xx-openjdk (YUM).
