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
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
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
$ sudo mv jdk1.6.0_07 /usr/local $ sudo ln -s /usr/local/jdk1.6.0_07 /usr/local/java
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
$ 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)
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!") |
javac command.$ cd /tmp $ javac Hello.java bash: javac: command not found
$ gedit ~/.bashrc
JAVA_HOME=/usr/local/java JAVA_BIN=$JAVA_HOME/bin PATH=$PATH:$JAVA_HOME:$JAVA_BIN export PATH
$ cd /tmp $ javac Hello.java $ java Hello Hello World!

Tidak ada komentar:
Posting Komentar