Installing Java Sun JDK on Ubuntu

This article will show how to install the Sun JDK 6 (Java Development Kit) on the Ubuntu 9.10 operating system. The installation of the JDK will be done within the user's home folder, the structure used is shown in the "Work area" post. If you are looking for the standard installation you will not find it here and I strongly recommend you to keep searching.

The JDK is mainly for developers; it is distributed along with library packages to develop software programs, utility tools like javac to compile Java source code to Java bytecode, java to execute this Java bytecode and many more contents bundled on it. Usually, each JDK contains one (or more) JRE (Java Runtime Environment), the JRE provides the minimum requirements for executing Java applications. It consists of a set of standard class libraries that implement the Java API (Application Programming Interface), the JVM (Java Virtual Machine), and supporting files. The Java bytecode is executed on the JVM. Since there are implementations of the JVM for different platforms (Linux, Solaris, Windows, etc), the source code can be compiled once and then executed independently on any of this platforms.

Environment

Required software that has already been installed and configured on the system.

  • Ubuntu 9.10 (fresh install).

Required files (~/downloads)

Downloaded files required during the installation process.

Installation steps

Step 1 - Unpack and install the bin file

Go to the folder where the file jdk-6u16-linux-i586.bin has been downloaded (e.g. ~/downloads). Change the file permissions to allow the execution and execute it. During the installation you will be prompted to accept the license terms (you better type "yes").

~$ cd downloads
~/downloads$ ls -l
-rw-r--r--  jdk-6u16-linux-i586.bin
~/downloads$ chmod a+x jdk-6u16-linux-i586.bin
~/downloads$ ./jdk-6u16-linux-i586.bin
[...]
I. Installation and Auto-Update.  The Software's
installation and auto-update processes transmit a limited
amount of data to Sun (or its service provider) about those
specific processes to help Sun understand and optimize
them.  Sun does not associate the data with personally
identifiable information.  You can find more information
about the data Sun collects at http://java.com/data/.

For inquiries please contact: Sun Microsystems, Inc., 4150
Network Circle, Santa  Clara, California 95054, U.S.A.

Do you agree to the above license terms? [yes or no]
yes
[...]
Java(TM) SE Development Kit 6 successfully installed.
[...]
Press Enter to continue.....
Done.
~/downloads$
Terminal

Step 2 - Move the folder to the desired location

Move the newly created folder from the unpacked file to the lab local folder (~lab/jvm/jdk1.6.0_16). Inside the lab folder will be installed all the tools, libraries and servers used on other tests, but you can move it anywhere, just make sure you adapt the tutorial accordingly. Next, create a symbolic link to facilitate further references to this folder, java-6-sun will be a good name. This link will also allow future updates of the JVM in a transparent manner; if all the configuration files are referencing to the symbolic link, it will only be necessary to update this link and point it to the newly created folder (e.g. ~ lab/jvm/jdk1.6.0_23).

~/downloads$ mkdir -p ~/lab/jvm
~/downloads$ mv jdk1.6.0_16 ~/lab/jvm
~/downloads$ ln -s ~/lab/jvm/jdk1.6.0_16 ~/lab/jvm/java-6-sun
Terminal

Step 3 - Configure environment variables

Below these lines you can see the current value of the JAVA_HOME and PATH environment variables in the system, notice that each folder is separated by a colon.

~$ echo $JAVA_HOME

~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Terminal

At this point, it is possible to compile Java source code and execute the generated bytecode either inside the ~/lab/jvm/java-6-sun/bin folder or using absolute paths. But we want to be able to call this commands from anywhere in the file system, so we need to initialize the JAVA_HOME environment variable and append the path to the Java's bin folder inside the PATH environment variable. To do so, we can edit the ~/.profile file or ~/.bashrc file. The difference between these files is that the former is loaded once when you log in, whereas the latter is loaded every time you start a new terminal.

Add the following lines at the end of the file.

# Java environment variables
export JAVA_HOME=$HOME/lab/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin
~/.profile

Log out to apply the changes. To check that the new values has been added to the JAVA_HOME and PATH environment variables, execute the previous commands.

~$ echo $JAVA_HOME
/home/me/lab/jvm/java-6-sun
~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/me/lab/jvm/java-6-sun/bin
Terminal

From now on, the java and javac (among other utilities inside the Java's bin folder) can be executed from any folder on the file system. Execute this command from the user's home folder: "java -version", this should display the information about the product version.

~$ java -version
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)
Terminal

Test

For testing the newly installed JDK we will write a simple Java program. Create a text file called SimpleTest.java and put the following code inside.

public class SimpleTest {
   public static void main(String[] args) {
      System.out.println("This works!");
   }
}
SimpleTest.java

Save it somewhere in the file system and then compile it using the javac command. If everything works properly it should exist a new file called SimpleTest.class inside the current folder. Launch it using the java command.

~$ javac SimpleTest.java
~$ java SimpleTest
Hello World!
Terminal

References

2 comments

John said...

For someone who had been exiled to Windows for many years, this was very helpful and reassuring. Thank you!

Raimon said...

Thanks John!

Note that this is not the most straight forward way to install Java, neither the typical one.

I will take this opportunity to list the changes necessaries, based on this post, not to make the installation easier, but more standard at least (system wide).

Step 2 - Move the folder to the desired location (root privileges required)
~/downloads$ sudo mkdir -p /usr/lib/jvm
~/downloads$ sudo mv jdk1.6.0_16 /usr/lib/jvm
~/downloads$ sudo ln -s /usr/lib/jvm/jdk1.6.0_16 /usr/lib/jvm/java-6-sun

Step 3 - Integration of Java into the system
~/$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-6-sun/bin/java 10
~/$ java -version
java version "1.6.0_16"[...]

Enjoy!

Post a Comment

Top