5/7/08

How to setup the Java JDK on Ubuntu Server

This short post is intended to show how I set up the Sun Java JDK on a fresh install of Ubuntu Server.

Step1. Download the latest JDK from Sun into your home directory. At the time of this post the latest JDK filename was

jdk-6u6-linux-i586.bin

Step2. Make the file executable and run
> chmod +x jdk-6u6-linux-i586.bin
> ./jdk-6u6-linux-i586.bin

Step3. Once it's finished running you will have a directory called jdk1.6.0_06/

Step4. Now make a directory under /usr called java_jdks and move the JDK directory into it.
> mkdir /usr/java_jdks
> mv ./jdk1.6.0_06 /usr/java_jdks

Step5. Make a symlink in /usr called java that points to the JDK you just copied.
> ln -s /usr/java_jdks/jdk1.6.0_06 /usr/java

Step6. Create a file called java.sh in /etc/profile.d containg these lines.

export JAVA_HOME=/usr/java
export PATH=$PATH:$JAVA_HOME/bin

This will set the JAVA_HOME variable which is useful for things like Tomcat and add Java's bin directory to your path.

Step7. Logout and log back in to get the new environment variables and test it out.
> java -version
java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)


Voila, you're done :)

Note: when it comes time to upgrade the JDK to a new version all you need to do is unpack the file and move the JDK directory to /usr/java_jdks and update the symlink to point to the new JDK directory.

You'll have to copy over any jars you dropped into $JAVA_HOME/jre/lib/ext to the new JDK but you shouldn't be using the /ext directory in the first place.