Post

How to Install Java JDK on Linux



Introduction

Installing the Java Development Kit (JDK) on a Linux system is a straightforward process, and while the exact steps may vary based on your Linux distribution, the general approach remains consistent across different versions of the JDK. This guide will walk you through the process of downloading, installing, and configuring the JDK on a Linux system, ensuring you’re ready for Java development.

Step 1: Download the JDK

To begin, you’ll need to download the appropriate version of the JDK for your Linux distribution.

  1. Visit the Download Page
    Navigate to the Oracle JDK download page or OpenJDK.
  2. Select the Version
    Choose the version of the JDK that you want to install. Common versions include JDK 8, JDK 11, and JDK 17. It’s generally a good idea to select the latest Long-Term Support (LTS) version.
  3. Download the Linux Installer
    Look for the tar.gz file or RPM package suitable for your distribution:
    • Debian/Ubuntu: Look for .deb files.
    • Fedora/Red Hat/CentOS: Look for .rpm files.
    • Alternatively, you can download a tar.gz file for manual installation.

Step 2: Install the JDK

2.1. For Debian/Ubuntu-based Distributions

  1. Open the Terminal
    You can usually find this in your applications menu or by pressing Ctrl + Alt + T.
  2. Update Package Index:
    1
    
    sudo apt update
    
  3. Install JDK
    If you downloaded a .deb file, you can install it using:
    1
    
    sudo dpkg -i path_to_your_file.deb
    

    Alternatively, you can install OpenJDK directly from the repository:

    1
    
    sudo apt install openjdk-17-jdk  # Replace with the desired version
    

2.2. For Fedora/Red Hat/CentOS-based Distributions

  1. Open the Terminal
  2. Install JDK
    If you downloaded a .rpm file, install it using:
    1
    
    sudo rpm -ivh path_to_your_file.rpm
    

    Alternatively, install OpenJDK from the repository:

    1
    
    sudo dnf install java-21-openjdk-devel  # Replace with the desired version
    

2.3. For tar.gz installation (all distributions)

  1. Extract the tar.gz File
    1
    
    tar -xvzf jdk-17_linux-x64_bin.tar.gz  # Replace with your file name
    
  2. Move it to a Suitable Directory
    1
    
    sudo mv jdk-17 /usr/local/
    

Step 3: Configure Environment Variables

After installation, you need to set the JAVA_HOME environment variable and update the PATH to use the JDK.

  1. Open the Terminal.
  2. Edit the Profile File
    • For system-wide changes, edit /etc/profile:
      1
      
      sudo nano /etc/profile
      
    • For user-specific changes, edit ~/.bashrc or ~/.bash_profile:
      1
      
      nano ~/.bashrc
      
  3. Add the Following Lines
    1
    2
    
    export JAVA_HOME=/usr/local/jdk-17  # Replace with your JDK path
    export PATH=$JAVA_HOME/bin:$PATH
    
  4. Save and Close the File
    If using nano, press Ctrl + X, then Y, and Enter.
  5. Apply the Changes
    1
    
    source ~/.bashrc  # or source /etc/profile for system-wide changes
    

Step 4: Verify the Installation

To verify that the JDK is correctly installed and configured, run the following commands in the terminal:

  1. Check the Java Version
    1
    
    java -version
    

    You should see the version of Java that you installed.

  2. Check the Java Compiler (javac)
    1
    
    javac -version
    

    This will confirm that the Java compiler is available and working.

If both commands return the expected version numbers, your JDK installation is complete.

Additional Notes: Processor Architectures

When downloading the JDK, ensure you choose the correct version for your system’s architecture:

  • x64
    Most modern systems run on 64-bit architecture. If you are using a 64-bit version of Linux, download the x64 version of the JDK.
  • ARM
    If you are on a newer ARM-based Linux system, ensure you download the appropriate ARM version of the JDK, as many distributions provide builds tailored for ARM processors.

To check your system’s architecture, you can run:

1
uname -m

This command will return x86_64 for 64-bit or arm64 for ARM.

Troubleshooting

  • Java Not Found
    Ensure that the JAVA_HOME and PATH variables are correctly set and that you have reloaded your profile.
  • Permission Issues
    Use sudo for commands that require administrative privileges.

Conclusion

Installing the Java JDK on Linux is a straightforward process, with similar steps across different distributions. By downloading the appropriate installer, running it, and configuring environment variables like JAVA_HOME and PATH, you will be set up for Java development.

Be sure to choose the correct JDK build based on your system architecture to avoid any compatibility issues. With the JDK successfully installed, you’re now ready to start developing Java applications on your Linux system!

© 2024 Java Tutorial Online. All rights reserved.