Saturday, April 4, 2009

How to install Java JDK 1.6 on Ubuntu

Hello friends,

I did this tutorial for a friend who needed to install Java JDK on Ubuntu.

As you will see, it's a little long process, but thanks to my curiosity I noticed that Ubuntu has a package ready to install using your package manager.

The java command to install it:

$sudo apt-get install sun-java6-jdk

The good thing is that Ubuntu has packages for things like flash and other softwares that are not easy to install on other distributions.

For some who want to try the long way here it is :)

1. Download the Java JDK 1.6 from Sun's site. http://java.sun.com/javase/downloads/index.jsp

2. Select the Java SE Development Kit (JDK)

3. This leads to a page allowing us to select the Operating System, usually I just choose Linux :).

4. Then we came to a page which shows the available packages. I click on jdk-6u13-linux-i586.bin because this version is a package resembling to a tar.gz

5. Once the newly downloaded file is here, we need to extract it to a folder named jdk1.6.0_13 (for example).

6.It's better then to move  Jdk1.6.0_13 folder to /usr/local which is the standard folder for programs or GUI console. if you have problems copying the JDK, you can call with the command nautilus

$sudo nautilus

This way you can access the folder /usr/local.

7. We will proceed to add the JAVA_HOME variable to the system. To do this, open bash.bashrc file with the following command:

$sudo vi /etc/bash.bashrc

At the end of the file put the following instructions.

export JAVA_HOME=/usr/local/jdk1.6.0_13
export PATH=$JAVA_HOME/bin:$PATH

This makes sure that the JAVA_HOME variable is always available in the system.

8. Open a new console and run the command:

$java -version

To which we assume the following result:

java version "1.6.0_13"
Java (TM) SE Runtime Environment (build 1.6.0_13-b04)
Java HotSpot (TM) Server VM (build 11.2-b01, mixed mode)

If it does not work: (good time to dig into Linux privileges). Here is the script you may need to use.

sudo chmod 777 /usr/local/jdk1.6.0_13
sudo chmod -R 666 /usr/local/jdk1.6.0_13
sudo chmod 777 /usr/local/jdk1.6.0_13/bin
sudo chmod 777 /usr/local/jdk1.6.0_13/db
sudo chmod 777 /usr/local/jdk1.6.0_13/demo
sudo chmod 777 /usr/local/jdk1.6.0_13/include
sudo chmod 777 /usr/local/jdk1.6.0_13/jre
sudo chmod 777 /usr/local/jdk1.6.0_13/lib
sudo chmod 777 /usr/local/jdk1.6.0_13/man
sudo chmod 777 /usr/local/jdk1.6.0_13/sample
sudo chmod 777 /usr/local/jdk1.6.0_13/bin/*

As you can see I'm putting 777 permission to all first level folders + files into bin directory, this leaves the rest with 666.

That would be everything. Until next time maybe :)

9 comments:

aelgali said...

You broke it down very well sister. thanks for the solid guide.

yetanothersteve said...

Thanks for showing the long way to install it as well, since I want to be in control of the java version. To activate the java plugin in firefox, I also created a symbolic link.
$ cd /usr/lib/mozilla/plugins
$ sudo ln -s /usr/local/jdk1.6.0_13/jre/lib/amd64/libnpjp2.so

Anonymous said...

I followed your instructions. I put the

export JAVA_HOME=/usr/local/jdk1.6.0_13
export PATH=$JAVA_HOME/bin:$PATH

and this is how my bash.bashrc looks now



# System-wide .bashrc file for interactive bash(1) shells.

# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
# ;;
#*)
# ;;
#esac

# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi

# sudo hint
if [ ! -e $HOME/.sudo_as_admin_successful ]; then
case " $(groups) " in *\ admin\ *)
if [ -x /usr/bin/sudo ]; then
cat <<-EOF
To run a command as administrator (user "root"), use "sudo ^command^".
See "man sudo_root" for details.

EOF
fi
esac
fi

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
else
return 127
fi
}
fi
export JAVA_HOME=/usr/local/jdk1.6.0_13
export PATH=$JAVA_HOME/bin:$PATH



and I used the chmod also.

But the command $java -version returns this :


$ java -version
The program 'java' can be found in the following packages:
* gij-4.3
* java-gcj-compat-headless
* openjdk-6-jre-headless
* cacao
* gij-4.2
* jamvm
* kaffe
Try: sudo apt-get install ^selected package^
bash: java: command not found


Please help!

thirdy said...

thanks, mentioning to put the jdk folder /usr/local/ makes this guide better than others

Patel Vintesh said...

thanks buddy good one............

Patel Vintesh said...

thanks buddy...........

Pratik Soni said...

Thank you very much for the long method. I was looking for it from a quite long time.

Jeroen said...

Excellent and easy writeup. In my case I had to reboot before I saw the right Java version in step 8.

Cheers,

Jeroen

Shankao said...

Great!! Thankx!!