Thursday, June 11, 2009

Java in Ubuntu

Well, I am now just trying to use Ubuntu as a development environment. First thing I noticed is that there are more than one jre already installed by default in the system, and here is the evidence:

jars@jars-desktop:~$ locate /rt.jar

I really wanted to use the Sun JDK for my development instead of OpenJDK, so I installed the Sun Java.

Just type the command below and compare the results:

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

jars@jars-desktop:/etc/alternatives$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
sun-java6-bin is already the newest version.
sun-java6-jre is already the newest version.
sun-java6-jre set to manually installed.
Suggested packages:
sun-java6-demo sun-java6-doc sun-java6-source
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 14 not upgraded.
Need to get 17.4MB of archives.
After this operation, 55.7MB of additional disk space will be used.
Get:1 hardy-updates/multiverse sun-java6-jdk 6-07-3ubuntu2 [17.4MB]
Fetched 17.4MB in 3min23s (85.6kB/s)
Preconfiguring packages ...
Selecting previously deselected package sun-java6-jdk.
(Reading database ... 139447 files and directories currently installed.)
Unpacking sun-java6-jdk (from .../sun-java6-jdk_6-07-3ubuntu2_i386.deb) ...
sun-dlj-v1-1 license has already been accepted
Setting up sun-java6-jdk (6-07-3ubuntu2) ...

But which of the two JVMs that now reside in my system area going to be used? Here it comes the ass-saving (and brand new to me) Ubuntu command:

sudo update-java-alternatives -s java-6-sun

This will define Sun Java-6 as the default JVM in your system. If you prefer to do it step-by-step just type this:

jars@jars-desktop:~$ sudo update-alternatives --config java

There are 2 alternatives which provide `java'.

Selection Alternative
+ 1 /usr/lib/jvm/java-6-openjdk/jre/bin/java
* 2 /usr/lib/jvm/java-6-sun/jre/bin/java

Press enter to keep the default[*], or type selection number:

The next tricky step is to set the prefered order in which your JVMs shoud be used. To set this up just edit the /etc/jvm file and include your recently installed JVM as the first one, so your file looks more or less like this:

# This file defines the default system JVM search order. Each
# JVM should list their JAVA_HOME compatible directory in this file.
# The default system JVM is the first one available from top to
# bottom.


Ok, not ready yet, but I am not willing to return to Windows without doing this until the end! :)
So, next step is setup the environment variables and if you want to set it system wide, edit the /etc/profile file and append the following lines at the end:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

You can also set the variables for only one user by editing /home/$user_name$/profile.

Now test it!!

jars@jars-desktop:/etc/alternatives$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

You find this article also on

1 comment:

Daniel said...

By the way, to install the source code for sun-java6 just execute the command:

sudo apt-get install sun-java6-source