fd Blog

Daniel Hilgarth on software development

Install Java on Linux

The following instructions detail how to install Java (JRE or JDK) on a Ubuntu server. 1. Get the download URL of the .tar.gz archive of the version you want. If you want to install the JRE, you can get the URL from here. For the JDK, you can get it from here. For the JDK, make sure you first accept the license agreement, before getting the URL from the page.

  1. Download the file
    • For the JRE:

      wget <URL> -O jre.tar.gz
      
    • For the JDK:

      wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "<URL>" -O jdk.tar.gz
      
  2. Extract the downloaded file:
    • JRE:

      tar zxvf jre.tar.gz
      
    • JDK:

      tar zxvf jdk.tar.gz
      
  3. Move the extracted folder to /usr/lib/jvm. Replace jre1.7.0_65 with the actual folder that has been created by the extraction.

    sudo mkdir -p /usr/lib/jvm ; sudo mv jre1.7.0_65/ /usr/lib/jvm
    
  4. Actually install Java:
    1. Get the existing installations: sudo update-alternatives --config java. This will give a list of choices:

      $ sudo update-alternatives –-config java
      There are 2 choices for the alternative java (providing /usr/bin/java).
      
      Selection Path Priority Status
      
      0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode
      1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode
      2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
      

      Remember the greatest number and add 1, so in this example, you would remember 3.

      If you don’t get a list of choices you probably get an error message:

      update-alternatives: error: no alternatives for java

      That’s fine, you don’t have java installed so far. Just remember 1 in this case.

      Or you might get:

      There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/jdk1.7.0_67/bin/java Nothing to configure.

      In that case, remember 2.

    2. Now, execute:

      sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.7.0_65/bin/java <number you remembered>
      

      Again, replace jre1.7.0_65 with the actual folder name.

    3. If you are installing the JDK, execute the following, too:

      sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jre1.7.0_65/bin/javac <number you remembered>
      
    4. Fix the ownership and permissions:

      sudo chmod a+x /usr/bin/java
      # If you are installing the JDK:
      # sudo chmod a+x /usr/bin/javac
      sudo chown -R root:root /usr/lib/jvm/jre1.7.0_65
      
    5. Set JAVA_HOME environment variable:

      sudo nano /etc/profile.d/java.sh
      

      Add the following lines at the end of the file:

      export JAVA_HOME=/usr/lib/jvm/jre1.7.0_65
      export PATH=$JAVA_HOME/bin:$PATH
      

      If the file didn’t exist yet, then that’s perfectly fine. Just create it and set the correct ownership and permissions:

      chown root:root /etc/profile.d/java.sh
      chmod 755 /etc/profile.d/java.sh
      
    6. Verify Java has been installed correctly by executing java -version:

      $ java -version
      java version "1.7.0_25"
      Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
      
    7. You might get the following error:

      -bash: /usr/bin/java: No such file or directory

      In this case you probably installed the 32 bit version of Java on a 64 bit OS.
      You should be able to solve this problem by executing:

      sudo apt-get install libc6-i386
      

      After the installation finished java -version should work now.

References:

Comments