The Java Development Kit (JDK) is an essential tool for Java developers, providing the necessary components to compile, debug, and run Java applications. While OpenJDK is readily available in most package repositories, you might sometimes need to install the official Oracle JDK (formerly SUN Java) for specific compatibility requirements or access to proprietary features. This guide outlines how to install the Oracle JDK on Ubuntu using a .tar.gz archive, as it’s often not directly available through apt-get.

Why this method?

Sometimes, the desired version of the Oracle JDK isn’t directly available through the standard Ubuntu package manager (apt-get). This method allows you to install specific versions of the JDK, offering more control over your Java environment.

Prerequisites

  • A downloaded .tar.gz archive of the Oracle JDK (e.g., jdk-8uXXX-linux-x64.tar.gz). You can obtain this from the Oracle website (you may need an Oracle account).
  • sudo privileges on your Ubuntu system.

Step-by-Step Installation

  1. Create a Directory for Java:

    We’ll create a dedicated directory to house the JDK installation. A common location is /usr/lib/jvm.

    ahmed@ahmed-server:~/sun-java# mkdir -p /usr/lib/jvm/
    
  2. Extract the Archive:

    Move the downloaded .tar.gz archive to a suitable location (e.g., your home directory or a dedicated ~/sun-java directory). Then, extract the archive into the directory created in the previous step.

    ahmed@ahmed-server:~/sun-java# tar xvzf jdk1.7.0_75.tgz -C /usr/lib/jvm/
    

    Note: Replace jdk1.7.0_75.tgz with the actual name of your downloaded file.

  3. Setting Alternatives (Important for System-Wide Access):

    The update-alternatives command is crucial for managing multiple Java installations on your system. It allows you to specify which Java version should be used by default. We’ll configure alternatives for java, javac, and javaws.

    ahmed@ahmed-server:~/sun-java# sudo update-alternatives \
                           --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_75/bin/java" 1
    ahmed@ahmed-server:~/sun-java# sudo update-alternatives \
                           --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_75/bin/javac" 1
    ahmed@ahmed-server:~/sun-java# sudo update-alternatives \
                           --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_75/bin/javaws" 1
    

    Note: Adjust the paths (e.g., /usr/lib/jvm/jdk1.7.0_75/bin/java) to reflect the actual installation directory of your JDK version. The 1 at the end of each command represents the priority. Higher priority means it will be preferred if set to auto mode.

    You might see output similar to this:

    update-alternatives: using /usr/lib/jvm/jdk1.7.0_75/bin/javaws to provide /usr/bin/javaws (javaws) in auto mode.
    
  4. Set Permissions:

    Ensure the Java executables have the correct permissions for execution. We also set the ownership to root for security.

    ahmed@ahmed-server:~/sun-java# sudo chmod a+x /usr/bin/java
    ahmed@ahmed-server:~/sun-java# sudo chmod a+x /usr/bin/javac
    ahmed@ahmed-server:~/sun-java# sudo chmod a+x /usr/bin/javaws
    ahmed@ahmed-server:/usr/lib/jvm# sudo chown -R root:root /usr/lib/jvm/jdk1.7.0_75
    
  5. Configure Alternatives (Selecting the Default Java):

    This step lets you choose which Java version to use as the system default.

    ahmed@ahmed-server:/usr/lib/jvm# sudo update-alternatives --config java
    

    You’ll be presented with a list of installed Java versions. Enter the number corresponding to the Oracle JDK you just installed (in this example, 3).

    There are 3 choices for the alternative java (providing /usr/bin/java).
    
      Selection    Path                                            Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode
      1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
      2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode
      3            /usr/lib/jvm/jdk1.7.0_75/bin/java                1         manual mode
    
    Press enter to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/lib/jvm/jdk1.7.0_75/bin/java to provide /usr/bin/java (java) in manual mode.
    

    Repeat this process for javac:

    ahmed@ahmed-server:/usr/lib/jvm# sudo update-alternatives --config javac
    

    Again, select the number corresponding to your Oracle JDK.

    There are 2 choices for the alternative javac (providing /usr/bin/javac).
    
      Selection    Path                                         Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1051      auto mode
      1            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1051      manual mode
      2            /usr/lib/jvm/jdk1.7.0_75/bin/javac            1         manual mode
    
    Press enter to keep the current choice[*], or type selection number: 2
    update-alternatives: using /usr/lib/jvm/jdk1.7.0_75/bin/javac to provide /usr/bin/javac (javac) in manual mode.
    

    The error for javawc is expected, as it’s not a standard alternative.

    ahmed@ahmed-server:/usr/lib/jvm# sudo update-alternatives --config javawc
    update-alternatives: error: no alternatives for javawc.
    
  6. Set the JAVA_HOME Environment Variable (Optional but Recommended):

    Setting JAVA_HOME makes it easier for applications to locate your Java installation. Edit your ~/.bashrc or ~/.profile file:

    nano ~/.bashrc
    

    Add the following lines to the end of the file, replacing /usr/lib/jvm/jdk1.7.0_75 with the actual path to your JDK installation directory:

    export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_75
    export PATH=$PATH:$JAVA_HOME/bin
    

    Save the file and source it to apply the changes:

    source ~/.bashrc
    

Verification

Finally, verify that the installation was successful by checking the Java version:

ahmed@ahmed-server:/usr/lib/jvm# java -version

You should see output similar to this:

java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)

And check the javac version as well:

ahmed@ahmed-server:/usr/lib/jvm# javac -version

Output:

javac 1.7.0_75

Troubleshooting

  • “Command not found” errors: Double-check that the PATH environment variable is correctly configured and includes the $JAVA_HOME/bin directory.
  • Incorrect Java version: Ensure you’ve correctly configured the alternatives using update-alternatives --config java and update-alternatives --config javac.
  • Permissions issues: Verify that the Java executables have execute permissions (chmod a+x) and that the installation directory is owned by root:root (chown -R root:root).

Conclusion

This guide provides a comprehensive walkthrough of installing the Oracle JDK on Ubuntu from a .tar.gz archive. By following these steps, you can ensure a properly configured Java environment for your development needs. Remember to adjust the paths and filenames to match your specific JDK version and installation location.