Saturday, June 11, 2011

Setting up Java in Linux

Setting up Java in Linux can be a real pain when you are an Ubuntu noob. It took me quite some time (nearly a day to be precise) to get it working. After a lot of purges, fresh installs and reboots, its successfully installed. I thought of sharing with you the goldmine "Java installation in Linux made easy" :P

I had initially installed Java from the official site by Oracle but it always gave me a lot of errors whenever I opened Eclipse, saying "Java 6 was not installed". I am sure there were many ways to work around it with a lot of commands etc. But, c'mon let's face it, we are Ubuntu noobs. So, I had to purge the whole installation and cleaned the entire system of Java before starting all over again. This time I used the method mentioned below and it worked.

Open the terminal and install sun java with the command

sudo apt-get install sun-java6-jdk sun-java6-jre

This should the whole sun java installation process. To be on the safer side, after this installation, open Synaptic Package Manager (System>Administrator>Synaptic Package Manager) and search for sun-java6-bin, sun-java6-jre, sun-java6-jdk and sun-java6-plugin. If they are already marked as installed, well and good, otherwise install them. They say the jdk does include the jre and you don't have to install both of them. But, I installed both.

JDK and JRE will mostly be installed at /usr/lib/jvm/java-6-sun-1.6.0.24. Ubuntu will create a symbolic link to java and javac at /usr/bin/java and /usr/bin/javac for easier access.

Next, we have to create a symbolic link to show where Java is installed in the system. This can be done with the following commands.

sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.24/bin/java /usr/bin/java
sudo ln -s /usr/lib/jvm/java-6-sun-1.6.0.24/bin/javac /usr/bin/javac

Lastly, you have to set the JAVA_HOME variable. This can be done by opening the .bashrc file in the home directory as superuser and editing by including this line at the end.


export JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.24"

Finally, you can check if Java is installed by typing the following command in the terminal


java -version

If it returns the version number of Java installed in your system, then Bingo! You have got Java successfully installed in your system.

Now, you can install your JDK SE or EE bundles from the Oracle site.

4 comments:

  1. why do we need ln -s and what is the difference between soft and hard links?

    ReplyDelete
  2. Why not use Open-JDK?

    ReplyDelete
  3. @Yours truly...hard links basically have the same inode number. Each hard link actually contains the same file contents. Deleting the original file doe not affect the hard link in any way.

    Soft links on the other hand are like shortcuts on Windows. It has different inode numbers but point to the original file. Deleting the original file makes a symbolic link a dangling one.

    A good aricle can be found here.

    ReplyDelete
  4. @Anonymous...I have not used OpenJDK on my machine but I think it should do the same thing considering it as an open source platform of Java SE. Do let me know by commenting here about it.

    ReplyDelete