Monday, September 3, 2012

Install Oracle JDK 7 on Ubuntu

A script to install Oracle JDK 7 on Ubuntu.

There are may resources on the web to install JDK 7 on Ubuntu/Debian.

This link for example, covers all uses cases, including browser plugins.

For basic requirements like running command line Java programs, this simple script works best for me:


JAVA_CURR_BIN=$(update-alternatives --query java | awk '/^Value/{print $2}')

if [[ ${JAVA_BIN} = ${JAVA_CURR_BIN} ]] ; then
  echo "Correct java already in use. Set java: ${JAVA_CURR_BIN}"
  echo "About to install: ${JAVA_BIN}"
  echo "Exit with nothing to do"

tar xzf jdk-7u7-linux-x64.tar.gz
mkdir -p /usr/lib/jvm/
rm -rf /usr/lib/jvm/jdk1.7.0
mv ./jdk1.7.0_07 /usr/lib/jvm/jdk1.7.0
update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1

# interactive:
#update-alternatives --config java

# non-interactive:
update-alternatives --set java ${JAVA_BIN}

JAVA_CURR_BIN=$(update-alternatives --query java | awk '/^Value/{print $2}')
echo "Installed java at ${JAVA_CURR_BIN}"

I use a modified version of the above script in a puppet recipe for automated installs.

No comments: