Minggu, 03 Januari 2010

Install Java on Ubuntu Linux

By default ubuntu comes with pre-installed Java Runtime Environment called gij (The GNU Java bytecode interpreter). But gij interpreter known to be not compatible with some Java code. So, most of linux user update their JVM to Sun JVM.
Basically we have two methods to install Java/JDK on ubuntu. The first is using apt-get command that will install Java through ubuntu repository, and the second one is manually install Java using traditional method “unpack and run”. The first method is the most easiest way to install Java, but the package(Java version) maybe quite out of date. My recommendation is to use the second method since it provide you the latest JDK version. Let’s try the first method.
Install using apt-get
Open your terminal(Application - Accessories - Terminal) then type:
$ sudo apt-get install sun-java6-jdk
Note: We are using sun-java6-jdk not sun-java6-jre since I assume we are going to use it to develop Java programs not only for running the programs.
Follow the instruction to complete the instalataion. Done? yeah what do you expect more? we are in debian world :). Then we need to check whether the Sun JVM is properly installed or not by typing:
$ java -version
You should get information about your current JVM. The JVM should provided by Sun NOT gij anymore. Now let’s move to the second method.
Install using “unpack and run” method
In this method off course you need to download the JDK package from Sun’s Website. Go to http://java.sun.com/ and grab the latest JDK version. In this tutorial I use JDK version 1.6.0 Update 7. Download the file jdk-6u7-linux-1586.bin and save it to some location e.g: /home/user/jdk-6u7-linux-1586.bin.
Open the terminal to extract the package, move to the saved location first:
$ cd /home/user
$ sh jdk-6u7-linux-1586.bin
You will be asked “License Agreement” and type yes to Agree. After you complete those steps a folder named “jdk1.6.0_07″ created. Now, I don’t want to run jdk from my home folder, so I move it to /usr/local. Since /usr/local is owned by root we need sudo command.
$ sudo mv jdk1.6.0_07 /usr/local
$ sudo ln -s /usr/local/jdk1.6.0_07 /usr/local/java
The second command ln -s is used to make symbolic link. Now you can also access /usr/local/jdk1.6.0_07 with /usr/local/java.
We have successfully install the new JDK, but when type java -version the current JVM is still gij. So, how do I change this? We need to tell the shell that we want to use our JDK located in /usr/local/java not the gij, for that purpose we use update-alternatives command.
$ update-alternatives --list java
/usr/bin/gij-4.2
After running that command we knew that there is no alternative for java. So, we need to add our new JDK to the alternatives. Since we are modifying the system, sudo command is needed to do so.
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/bin/java 100
$ sudo update-alternatives --config java
There are 2 alternatives which provide `java'.
 
  Selection    Alternative
-----------------------------------------------
*         1    /usr/bin/gij-4.2
 +        2    /usr/local/java/bin/java
 
Press enter to keep the default[*], or type selection number: 2
$ 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)
For more information about update-alternatives see the manual page man(1) update-alternatives.
Now let’s test our new environment, for this purpose we will create simple Java program. Open your favorite text editor, e.g gedit (Application - Accessories - Text Editor) and try this following code.
1
2
3
4
5
public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello World!") 
   }
}
Save it to some location, since this is only a test I would recommend to save it under /tmp directory. I name it Hello.java. Now go back to the terminal and compile the file using javac command.
$ cd /tmp
$ javac Hello.java
bash: javac: command not found
Oops…, what am I missing? No you don’t, the are few steps that we did not completed yet :). We need to add /usr/local/java in shell environment variables by editing .bashrc file located in our home directory.
$ gedit ~/.bashrc
Add the following lines into .bashrc (put these code at the end of file).
JAVA_HOME=/usr/local/java
JAVA_BIN=$JAVA_HOME/bin
PATH=$PATH:$JAVA_HOME:$JAVA_BIN
export PATH
Save the file and close the editor. Now we need to close our Terminal to take affect. Open the terminal again and try to compile Hello.java. All the things should work :).
$ cd /tmp
$ javac Hello.java
$ java Hello
Hello World!
Comment and feedback are welcome… :)

Tidak ada komentar:

Posting Komentar