rioastamal.net

Just things inside my head…

Tutorial: How to Install Java on Ubuntu Linux

Posted by rio On October - 2 - 2008

Sorry if there are any spelling or grammatical errors, I’m learning English… :)

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… :)

bookmark bookmark bookmark bookmark bookmark bookmark

  1. sawali tuhusetya Said,

    mohon maaf maun numpang OOT, Kami sekeluarga turut mengucapkan selamat hari raya idulfitri 1429 H, mohon maaf lahir dan batin. Semoga kita bisa kembali ke fitrah-Nya, amiin. Salam silaturahmi.

  2. Edi Psw Said,

    Wah, tutorial yang menarik nih.

    Btw, met idul fitri ya?
    Mohon maaf lahir & batin.

  3. fat loss 4 idiots Said,

    Hey more pics please, you must balance the text and the pictures :D

  4. rio Said,

    @fat_loss_4_idiots, yeah sorry.. I’m planning to add the video for this tutorial :)

  5. mbahsomo Said,

    Good Article pren

  6. rio Said,

    @mbahsono, thanks :)

  7. rioastamal.net » Blog Archive » Tutorial: How to Install Apache Ant on Ubuntu Linux Said,

    [...] You must have Java installed first, go to my last article if you have not install Java [...]

  8. Arman Said,

    sounds good. you have done a great work on this. i m not runing ubuntu but it helped little bit
    however, lookin at couple of thing you are missing ; on java test .. i just thought let you know

    thank you

  9. rio Said,

    @arman,

    yeah right I’m missing the “;” :)

    thanks

  10. rioastamal.net » Blog Archive » Tutorial: How to Install Google Android SDK on Ubuntu Linux Said,

    [...] How to Install Java on Ubuntu Linux [...]

  11. William Alves Said,

    Code in page, have error in copy paste, view sample here.

    public class Hello
    {
    public static void main( String[] args )
    {
    System.out.println( “Hello World!” );
    }
    }

  12. rio Said,

    @William,

    yeah there is no “;” at line 3.. just like what Arman said. Thanks

  13. Amar Elgali Said,

    Thanks alot bro, you broke it down well. i was looking for simple way to install jdk1.6 and you just got it right

  14. rio Said,

    @Amar,
    glad to know it helpful for you… :)

  15. grow taller 4 idiots Said,

    Nice article. I agree that social networking is the niche to be in. I dont know if it is even a niche. It is an enterprise of itself.

  16. vagourman Said,

    Very Good man , every little detail !
    You Rock !

Add A Comment