Wednesday, December 18, 2013

How to install Open JDK in Ubuntu


You can refer http://openjdk.java.net/ to know what is Open JDK. In this I will be installing Open jdk version 1.7.0_25
  1. You can download your referred version from http://www.oracle.com/technetwork/java/javase/downloads/index.html
     
  2. Once you download, uncompress the file using following command
    sudo tar -xvf jdk-7u25-<version>.tar.gz

    JDK 7 package is extracted into /jdk1.7.0_25 directory.

  3. It is better to move the JDK 7 directory to /usr/lib via following commands
            sudo mkdir -p /usr/lib/jvm
            sudo mv jdk1.7.0_25/ /usr/lib/jvm/jdk1.7.0_25
  1. Then run the following commands :
sudo update-alternatives --install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_25/bin/java” 1
sudo update-alternatives --install “/usr/bin/javac” “javac” “/usr/lib/jvm/jdk1.7.0_25/bin/javac” 1
sudo update-alternatives --install “/usr/bin/javaws” “javaws” “/usr/lib/jvm/jdk1.7.0_25/bin/javaws” 1
  1. Then configure for the open jdk
    For Java :
    sudo update-alternatives --config java
This will output the following
There are 5 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 /opt/java/jdk1.7.0_25/bin/java 1 manual mode
2 /opt/java/jre1.7.0_25/bin/java 1 manual mode
3 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
* 4 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1051 manual mode
5 /usr/lib/jvm/jdk1.7.0_25/bin/java 1 manual mode
You have to Press enter to keep the current choice[*], or type selection number. In here I will be entering * since I need the 4th option.
    For Javac :
    sudo update-alternatives --config javac
This will output the following
There are 3 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 manual mode
* 2 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1051 manual mode
3 /usr/lib/jvm/jdk1.7.0_25/bin/javac 1 manual mode
You have to Press enter to keep the current choice[*], or type selection number. In here I will be entering * since I need the 2nd option.

    For Javaws :
    sudo update-alternatives --config javaws
This will output the following
There are 3 choices for the alternative javaws (providing /usr/bin/javaws).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/javaws 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/javaws 1061 manual mode
* 2 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/javaws 1060 manual mode
3 /usr/lib/jvm/jdk1.7.0_25/bin/javaws 1 manual mode
You have to Press enter to keep the current choice[*], or type selection number. In here I will be entering * since I need the 2nd option.

  1. Now you can check the java version
    java -version
This will output the following if the installation is successful :
java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

  1. Also you have to set JAVA_HOME in .bashrc.
    Run the following command and open the .bashrc and give the correct path.
    gedit ~/.bashrc
    Add the following to the end of the file.
    export JAVA_HOME=/usr/lib/jvm/java-1.7.0_25
    export PATH=$PATH:$JAVA_HOME/bin
  2. Source the bash profile.
    source ~/.bashrc