Installing JDK on Ubuntu Linux

5 March 2012 By Nithya Vasudevan 4,995 views One Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

Environment Used

  • Java SE 6 Update 30
  • Ubuntu Linux version 11.10 32-bit

Downloading Java Development Kit (JDK):

You can download latest version of JDK from this location

http://www.oracle.com/technetwork/java/javase/downloads/index.html
We have used Java SE 6 Update 30. Click on the download link and it will take you to the download page.

Accept the License Agreement and choose x86 (32-bit) or x64 (64-bit) version depending on your OS.

Download the JDK suitable for your system requirement to a safe location on your machine. We saved it on our home folder (/home/ibytecode).

Installing Java Development Kit (JDK) – Single User

Step 1:

Open Terminal by clicking on Ubuntu icon on desktop sidebar and type terminal in search bar. Click on the terminal icon.

Step 2:

Go to the folder where you saved the downloaded jdk file. In this case it is folder ‘/home/ibytecode”. So type,

cd /home/<username>

Step 3:

Check whether the downloaded jdk bin file has executable permissions by using the command,

ls –l

Step 4:

As you can see, the file does not have ‘x (executable) permission set. So change the file permissions to make it executable by issuing the following command.

chmod 755 <filename>
(i.e.)
chmod 755 jdk-6u30-linux-i586.bin

Step 5:

Check the permissions again by typing “ls –l” to make sure it is set properly. Now run the bin file by typing the following command.

./ jdk-6u30-linux-i586.bin

Step 6:

Press Enter to finish the installation. The jdk is installed in our current directory (i.e.) /home/ibytecode. Type “ls –l” to see the jdk folder, in our case it is jdk1.6.0_30.

Step 7:

Go into that folder by typing this command,

cd jdk1.6.0_30

and type “ls –l” to see the various folders like ‘bin’, ‘jre’, ‘lib’ etc.

Installing JDK – System wide install

You need to have administrator privileges for this section. If you do not have admin privileges please refer to single user install mentioned above.
Steps 1 – 4 are the same as in Single user install.

Step 1

Before we run the executable file, go to folder where you would like to install jdk, like /opt or /usr/share etc. We use /opt folder.

cd /opt

Step 2

Now run the file in “sudo” (admin) mode by issuing the following command.

sudo ~/jdk-6u30-linux-i586.bin

“~” refers home directory (/home/ibytecode)

Step 3

Press Enter to finish the installation. The jdk installed directory is /opt/jdk1.6.0_30.

Setting Required Environment Variables for Java

  • The installed JDK must be configured to the environment so that the Java compiler (javac) and runtime (java) becomes available for compiling and running the Java application from any location.
  • In Linux environments, there are many places where we can set our environment variables.
    According to Ubuntu documentation on Environment Variables

According to Ubuntu documentation on Environment Variables (https://help.ubuntu.com/community/EnvironmentVariables) the preferred file and location for setting environment variables which should affect FOR A SINGLE USER is .pam_environment file in your home directory.
Whereas FOR SYSTEM-WIDE ENVIRONMENT variables the preferred file is /etc/environment.

SINGLE USER: ~/.pam_environment
SYSTEM WIDE: /etc/environment

You can use any text editor which you are comfortable with to modify the files. We will be using “vi” editor; if you want a GUI based editor you can use “gedit”. To use that, wherever we type “vi” just replace with “gedit”.

Single User Java Environment Variables

  1. We will set the environment variable JAVA_HOME (used by many applications which require java) to point to the jdk folder location and add the “bin” folder inside the jdk folder to the PATH environment variable.
  2. Now go to your home folder by typing

    cd ~

  3. Create a file by using vi

    vi .pam_environment

  4. Press ‘i’ for insert mode and type the following lines.

    JAVA_HOME=~/jdk1.6.0_30
    PATH DEFAULT=${PATH}:${JAVA_HOME}/bin

  5. To save the file in “vi” editor press “Esc” and then type “:wq”.
  6. Now logout and then login again. Go to terminal and type the following commands.

    javac –version
    java –verison

  7. You should get proper outputs with the installed java version numbers displayed. If you get any command not found errors, then the path is not set properly.

System Wide Java Environment Variables

  1. We will set the environment variable JAVA_HOME (used by many applications which require java) to point to the jdk folder location and add the “bin” folder inside the jdk folder to the PATH environment variable.
  2. Open terminal and open the file “/etc/environment” by using “vi” in “sudo” mode.

    sudo vi /etc/environment

  3. Press ‘i’ for insert mode and JAVA_HOME.

    JAVA_HOME=/opt/jdk1.6.0_30

  4. Append /opt/jdk1.6.0_30/bin to the PATH variable preceded by “:” as shown below.
  5. To save the file in “vi” editor press “Esc” and then type “:wq”.
  6. Now logout and then login again. Go to terminal and type the following commands.

    javac –version
    java –verison

  7. You should get proper outputs with the installed java version numbers displayed. If you get any command not found errors, then the path is not set properly as shown in 5.1 Step 7.
  8. javac is the primary Java compiler, included in the Java Development Kit (JDK) and
  9. java is the Java launcher/runtime engine.

Tags: , , , , ,