Installing Java JDK 6 on Ubuntu

This article will show you how to install the JDK 6 (Java Development Kit) on the Ubuntu 10.10 operating system. The installation of the JDK will be done within the user's home folder, the structure used in this post is shown at the previous post "A transportable work area". If you are looking for a standard and straightforward 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, 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 10.10

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-6u23-linux-i586.bin has been downloaded (e.g. ~/downloads). Change the file permissions to allow the execution and execute it.

me@rarejava:~$ cd downloads/
me@rarejava:~/downloads$ ls -l
total 82488
-rw-r--r-- 1 me me 84461758 2011-02-11 09:46 jdk-6u23-linux-i586.bin
me@rarejava:~/downloads$ chmod a+x jdk-6u23-linux-i586.bin
me@rarejava:~/downloads$ ./jdk-6u23-linux-i586.bin
[...]
Creating jdk1.6.0_23/jre/lib/plugin.jar
Creating jdk1.6.0_23/jre/lib/javaws.jar
Creating jdk1.6.0_23/jre/lib/deploy.jar

Java(TM) SE Development Kit 6 successfully installed.

Product Registration is FREE and includes many benefits:
* Notification of new versions, patches, and updates
* Special offers on Oracle products, services and training
* Access to early releases and documentation

Product and system data will be collected. If your configuration
supports a browser, the JDK Product Registration form will
be presented. If you do not register, none of this information
will be saved. You may also register your JDK later by
opening the register.html file (located in the JDK installation
directory) in a browser.

For more information on what data Registration collects and
how it is managed and used, see:
http://java.sun.com/javase/registration/JDKRegistrationPrivacy.html

Press Enter to continue.....

Done.
me@rarejava:~/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_23). 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-jdk 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).

me@rarejava:~/downloads$ mkdir -p ~/lab/jvm
me@rarejava:~/downloads$ mv jdk1.6.0_23/ ~/lab/jvm/
me@rarejava:~/downloads$ ln -s ~/lab/jvm/jdk1.6.0_23/ ~/lab/jvm/java-6-jdk
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-jdk/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-jdk
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 following commands.

~$ echo $JAVA_HOME
/home/me/lab/jvm/java-6-jdk
~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
:/home/me/lab/jvm/java-6-jdk/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.

me@rarejava:~$ java -version
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, 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

It works!

References

blog comments powered by Disqus

Feb14

Post Sharing