Install Sun / Oracle Java JDK under Ubuntu

wget -q --no-check-certificate -O - | bash -


# ensure that this script is run by root
if [ $(id -u) -ne 0 ]; then
  sudo $0

# install debconf
apt-get install -y debconf-utils

# create answer file
debconf-set-selections <<\EOF
oracle-java8-installer shared/accepted-oracle-license-v1-1 select true

# ensure that add-apt-repository is installed
. /etc/os-release
if [ "${ID}" == "debian" ] && [ "${VERSION_ID}" == "7" ] ; then
  # Debian Wheezy
  apt-get install -y python-software-properties
  # Ubuntu and Debian Jessie
  apt-get install -y software-properties-common

# add repository
add-apt-repository -y ppa:webupd8team/java

# fix distribution name on Debian
for FILE in $(find /etc/apt/sources.list.d/ -name "*webupd8team*.list"); do
  sed -i 's|wheezy|precise|g;s|jessie|trusty|g' ${FILE}

# update repository
apt-get update -qq

#  bypass the apt-cacher-ng proxy
echo ' "DIRECT";' > /etc/apt/apt.conf.d/99_oracle

# install
apt-get install -y oracle-java8-installer

# set Java environment variables
apt-get install -y oracle-java8-set-default

# set java 8 as default java version
update-java-alternatives -s java-8-oracle

# move java desktop symbols
[ -f /usr/share/applications/JB-mission-control-jdk8.desktop ] && sed -i 's|Categories=Development;Java;|Categories=Application;Network;|g' /usr/share/applications/JB-mission-control-jdk8.desktop

Install JDK under Debian 7 (minimal)
echo "deb precise main" > /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver hkp:// --recv-keys EEA14886
echo ' "DIRECT";' > /etc/apt/apt.conf.d/99_oracle
apt-get update
echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | debconf-set-selections
apt-get install -y oracle-java7-installer
apt-get install -y oracle-java7-set-default

Download JDK manually
# v7
# v8
# download
wget --continue --no-check-certificate --header "Cookie: oraclelicense=a" ${URL} -O /tmp/${URL##*/}


# fix repo key
apt-key adv --keyserver hkp:// --recv-keys EEA14886

OPTIONAL: Uninstall OpenJDK
sudo apt-get remove -y openjdk-7-jre-headless

Display your current java version
java -version

# configure systemwide profile
echo "export JAVA_HOME=/opt/jvm/current" > /etc/profile.d/

Links - HowTo, install Java under Ubuntu and Debian - webupd8team Java repository