Installing Oracle Java JDK 7 and setting JAVA_HOME in Ubuntu (Linux)
This post is not only for people who are new to Linux or Java but also quick notes for me to setup my environment next time I install my system or setup a virtual machine.
Installing OpenJDK can be very simple but not Oracle JDK. You can use one of the two following commands (depending on which version you which to install) to setup OpenJDK in your linux environment.
sudo apt-get install openjdk-6-jdk
sudo apt-get install openjdk-7-jdk
But I have always used Oracle Sun JDK for my development. While Oracle JDK (earlier Sun JDK) is under the Binary Code License (earlier Sun License), OpenJDK is under GPL with a linking exception. From JDK version 7, Oracle has planned to support OpenJDK and withdraw the Operating System Distributor License for Java. This has resulted in a withdrawal of Oracle JDK from the repositories of Linux distributions. Therefore you can’t just use apt-get to install Oracle JDK.
Here is the stackoverflow link to help you choose between the Oracle JDK or OpenJDK.
But users who prefer to use the Oracle JDK binaries over OpenJDK builds packaged in their Linux distributions of choice can of course download the package from http://oracle.com/java under the same terms as users on other platforms. Here is the direct link to JavaSE downloads.
Coming the point I have downloaded JDK 7 (update 4) for Linux x86 (32-bit) – file: jdk-7u21-linux-i586.tar.gz
Step 1: After downloading the file open the terminal and navigate to the file
tar -xf jdk-7u21-linux-i586.tar.gz
This will extract and create a jdk folder at your current path.
Step 2: Create a location to keep your new JDK . I prefer and usually use /usr/lib/jvm/
You may need root permission to create the /usr/lib/jvm (hence use sudo).
sudo mkdir /usr/lib/jvm
Step 3: Move the extracted jdk folder to /usr/lib/jvm/
sudo mv jdk1.7.0_21 /usr/lib/jvm/
Step 4: Now we have to setup our system to use refer to our new jdk
sudo update-alternatives --install "/usr/bin/java" "java"
sudo update-alternatives --config java
And also register Firefox Java Plugin
sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so"
"mozilla-javaplugin.so" "/usr/lib/jvm/jdk1.7.0_21/jre/lib/i386/libnpjp2.so" 1
sudo update-alternatives --config mozilla-javaplugin.so
ALL DONE. You can test your java install by
Here is a small screen capture of what happened on my screen when I went through above steps
Finally if you need to add JAVA_HOME variable, you can do so by adding it to the .bashrc file in your home directory
Open .bashrc file using an editor. If you use VI then
and add the following 2 lines in your .bashrc file.
There may be other ways to install, but this is what I have followed always.