Saturday, November 02, 2013

A Guide to Installing Java on Ubuntu 12.04

I recently re-installed my desktop and switched from Debian to Ubuntu (personal choice for my own reasons).  Doing a fair amount of Python lately, I have gotten to really like using the Community Edition of Pycharm.
Unfortunately, while Pycharm is pretty sweet, its also written in Java (not a favorite of mine).  So, with that tidbit of info, I will now go through the steps necessary to install and setup Java on Ubuntu 12.04.


The first thing your going to need to do is download the JDK from Oracle. After you download it, you will need to unpack it (below I used the name of the version I downloaded):

     tar -zxvf jdk-7u45-linux-x64.tar.gz

After you unpack the tarball, you will have a directory with the software in it (mine was named jdk1.7.0_45).  You will want to move the new directory to a usable location.  I moved mine to the /usr directory as such:

     sudo mv ./jdk1.7.0_45 /usr/

You will need to make sure to do this as sudo, as root owns /usr/.  

Next, you are going to want to set the JAVA_HOME variable, which is done in either your .profile or your .bashrc in your home directory:

     export JAVA_HOME=/usr/jdk1.7.0_45

Just for giggles, I added a path to the bin directory where the java executables are located.  To do this I simply edited my .profile and added "/usr/jdk1.7.0_45/bin" to the PATH variable.

You now have to execute the following:

     sudo update-alternatives --install "/usr/bin/java" "java" "/usr/jdk1.7.0_45/bin/java" 1
     sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/jdk1.7.0_45/bin/javac" 1
     sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/jdk1.7.0_45/bin/javaws" 1

Now, create a plugins directory for Mozilla:

     mkdir ~/.mozilla/plugins/

And create a link to the necessary file:

     ln -s /usr/jdk1.7.0_45/jre/lib/amd64/libnpjp2.so ~/.mozilla/plugins/

After that, your JDK is now installed.  Just a bit of informaiton. Some people have said that you don't need the "update-alternatives" lines in order to complete this, just the path addition.  Actually, for me, it did not work until I did those links.  

Enjoy and hope it works for you too.

3 comments:

Cd-MaN said...

Why not use OpenJDK-7, available in the Canonical repositories? As simple as 'sudo apt-get install openjdk-7-jre', no need to set environment variables, etc.

There used to be a stock warning in the IntelliJ products about OpenJDK (along the lines of "it's not guaranteed to work with OpenJDK"), but it has been recently removed or in the process of being removed.

On a sidenote: 12.04 on desktops? I understand keeping servers at the LTS versions, but for desktops/laptops I prefer upgrading to the latest release.

Numberwhun said...

Ok, in my defense, when I downloaded the iso from Ubuntu, I only saw the top entry for 12.04 LTS. If Ubuntu were smarter, they would have put the 13.10 link on top, not underneath it for people to competely miss. Either way though, I did upgrade it via apt (watching the plethora of files update and install).
As for the java version, yes, I could have installed the openjdk, you are absolutely correct, but I wanted to install and use the Oracle version for this as it allowed me to now only get it installed and working, but also to learn the way to do it and write this blog post.
Thanks for the additional install notes though. I guess in retrospect, has anyone seen any major differences using openjdk over Oracle Java JDK? I ask as I have a couple of apps which require the JDK and want to make sure that there isn't any conflicts.

Cd-MaN said...

Perhaps Canonical want to publicize the LTS version for whatever reason. I have no insight into that :-)

As for Oracle vs. OpenJDK: I personally didn't witness any incompatibilities (and I use Java relatively frequently). I heard accounts of such incompatibilities, so there exists, but they are not common. I guess the main thing you get by using the Oracle branch of the JDK/JRE is support.

Cheers!

 
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.