Step-by-Step Guide to Install Java (JDK) on Different OS

Estimated reading: 5 minutes 57 views

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.

Install Java on Windows

Step 1: Download the JDK

  1. Go to the official Oracle JDK download page.
  2. Under the "Java SE" section, select the version you want to install (e.g., JDK 17).
  3. Scroll down to the "JDK Downloads" section and click on the Windows link (choose the appropriate version for your system: x64 or x86).
  4. Accept the Oracle License Agreement and download the Windows Installer (.exe) file.

Step 2: Install the JDK

  1. Double-click the downloaded .exe file to start the installation process.
  2. 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
  3. 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.

  1. 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.
  2. 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:

  1. Open Command Prompt (Win + R → type cmd → press Enter).
  2. Type the following commands:
    java -version
    javac -version

    These commands should return the version of Java installed on your system (e.g., java version "17").

Step 1: Download the JDK

  1. Visit the Oracle JDK download page.
  2. Under "Java SE", choose the version you want to install (e.g., JDK 17).
  3. Scroll down and click on the macOS link to download the appropriate .dmg file for your system.

Step 2: Install the JDK

  1. Double-click the downloaded .dmg file to mount the disk image.
  2. Open the mounted disk image and double-click the JDK installer package (.pkg).
  3. Follow the installation steps and use the default installation path (usually /Library/Java/JavaVirtualMachines).
  4. Once installation is complete, you can close the installer.

Step 3: Set Environment Variables

  1. Open Terminal (use Cmd + Space, type "Terminal", and press Enter).

  2. Edit the ~/.bash_profile (or ~/.zshrc for Zsh users) file to set JAVA_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 press Y to confirm).
  3. Apply the changes by running:

     
    source ~/.bash_profile

Step 4: Verify Installation

  1. 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)

  1. Open Terminal.
  2. Update your package index:
     
    sudo apt update
  3. Install the OpenJDK package (open-source version of Java):
     
    sudo apt install openjdk-17-jdk

Step 2: Verify Installation

  1. 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:

  1. Open Terminal and edit your ~/.bashrc (or ~/.zshrc for Zsh users) file:

     
    nano ~/.bashrc
  2. 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.
  3. Save and close the file (Ctrl + X, then Y to confirm).

  4. Apply the changes:

     
    source ~/.bashrc
  5. 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:

  1. Download the Oracle JDK from the Oracle Downloads page.
  2. Extract the archive to /opt/ or another directory:
     
    sudo tar -xvzf jdk-17_linux-x64_bin.tar.gz -C /opt/
  3. 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
  4. Add the above lines to your ~/.bashrc or ~/.zshrc file, then run source ~/.bashrc to apply the changes.

Conclusion

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!

Leave a Comment

Share this Doc

Step-by-Step Guide to Install Java (JDK) on Different OS

Or copy link

CONTENTS