Step-by-Step Guide to Install Java (JDK) on Different OS
Java is one of the most popular programming languages in the world, and setting up the Java Development Kit (JDK) is the first step to beginning your journey as a Java developer. The JDK provides everything you need to create Java applications, including the Java compiler, runtime environment, libraries, and other tools. Here’s a detailed guide for installing the JDK on Windows, macOS, and Linux systems.
Step 1: Download the JDK
- Go to the official Oracle JDK download page.
- Under the "Java SE" section, select the version you want to install (e.g., JDK 17).
- Scroll down to the "JDK Downloads" section and click on the Windows link (choose the appropriate version for your system: x64 or x86).
- Accept the Oracle License Agreement and download the Windows Installer (.exe) file.
Step 2: Install the JDK
- Double-click the downloaded .exe file to start the installation process.
- Follow the on-screen instructions. You can accept the default installation path or specify a different one. The default path is typically:
C:\Program Files\Java\jdk-17
- Click Next to proceed through the steps and finish the installation.
Step 3: Set Environment Variables
After installing the JDK, you need to configure your environment variables so that Java tools (like javac
and java
) are accessible from the command line.
- Set JAVA_HOME:
- Open the Start Menu, search for "Environment Variables", and click on "Edit the system environment variables".
- In the System Properties window, click on Environment Variables.
- Under System Variables, click on New to add a new variable.
- Variable Name:
JAVA_HOME
- Variable Value: The path to your JDK installation, e.g.,
C:\Program Files\Java\jdk-17
.
- Variable Name:
- Update PATH:
- In the same Environment Variables window, scroll down and select the Path variable under System variables and click Edit.
- Click New and add the following path:
C:\Program Files\Java\jdk-17\bin
- Click OK to close all windows.
Step 4: Verify Installation
To confirm that Java has been installed correctly:
- Open Command Prompt (
Win + R
→ typecmd
→ press Enter). - Type the following commands:
java -version
javac -versionThese commands should return the version of Java installed on your system (e.g.,
java version "17"
).
Step 1: Download the JDK
- Visit the Oracle JDK download page.
- Under "Java SE", choose the version you want to install (e.g., JDK 17).
- Scroll down and click on the macOS link to download the appropriate
.dmg
file for your system.
Step 2: Install the JDK
- Double-click the downloaded
.dmg
file to mount the disk image. - Open the mounted disk image and double-click the JDK installer package (
.pkg
). - Follow the installation steps and use the default installation path (usually
/Library/Java/JavaVirtualMachines
). - Once installation is complete, you can close the installer.
Step 3: Set Environment Variables
Open Terminal (use
Cmd + Space
, type "Terminal", and press Enter).Edit the
~/.bash_profile
(or~/.zshrc
for Zsh users) file to setJAVA_HOME
:nano ~/.bash_profile
- Add the following line:
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export PATH=$JAVA_HOME/bin:$PATH
- Save the file and exit the editor (
Ctrl + X
, then pressY
to confirm).
- Add the following line:
Apply the changes by running:
source ~/.bash_profile
Step 4: Verify Installation
- In Terminal, type the following commands:
java -version
javac -version
You should see the version of Java you've installed (e.g.,
java version "17"
).
Step 1: Install JDK Using Package Manager (Ubuntu/Debian)
- Open Terminal.
- Update your package index:
sudo apt update
- Install the OpenJDK package (open-source version of Java):
sudo apt install openjdk-17-jdk
Step 2: Verify Installation
- After installation is complete, verify the JDK version:
java -version
javac -version
These commands should show the installed version of Java (e.g.,
openjdk version "17"
).
Step 3: Set Environment Variables (Optional)
If you need to set JAVA_HOME
and update PATH
, follow these steps:
Open Terminal and edit your
~/.bashrc
(or~/.zshrc
for Zsh users) file:nano ~/.bashrc
Add the following lines to the file:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
- Adjust the path if you're using a different version or distribution.
Save and close the file (
Ctrl + X
, thenY
to confirm).Apply the changes:
source ~/.bashrc
Verify the environment variables are correctly set:
echo $JAVA_HOME
java -version
Step 4: Install JDK from Oracle (Alternative Method)
If you want to install the official Oracle JDK (instead of OpenJDK), follow these steps:
- Download the Oracle JDK from the Oracle Downloads page.
- Extract the archive to
/opt/
or another directory:sudo tar -xvzf jdk-17_linux-x64_bin.tar.gz -C /opt/
- Set up the
JAVA_HOME
environment variable to point to the extracted directory:export JAVA_HOME=/opt/jdk-17
export PATH=$JAVA_HOME/bin:$PATH
- Add the above lines to your
~/.bashrc
or~/.zshrc
file, then runsource ~/.bashrc
to apply the changes.
By following the steps above, you can install the Java Development Kit (JDK) on your system, regardless of whether you’re using Windows, macOS, or Linux. Once you’ve installed the JDK, you can start developing Java applications using the tools provided by the JDK, such as the compiler (javac
), runtime environment (java
), and development libraries.
Remember to verify your installation by checking the Java version using the java -version
and javac -version
commands. You are now ready to begin your journey as a Java developer!