ubuntu上安装Oracle JDK

以前写了一个为CentOS安装JDK的脚本,每次安装跑一次脚本就行了。
今天在ubuntu上安装JDK,才发现ubuntu好讨厌,相异的地方太多了

ubuntu已经从源中移除了Oracle JDK,用apt-get再也安装不了,只能手动下载安装
Oracle JDK以前还可以直接用wget下载,大概半年前起多了cookies认证,否则不给下
两家都是超讨厌的公司

用wget下载时在header中插入cookie即可

cd /home/ubuntu
wget -O jdk-6u34-linux-x64.bin --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk6-downloads-1637591.html;" http://download.oracle.com/otn-pub/java/jdk/6u34-b04/jdk-6u34-linux-x64.bin
chmod +x jdk-6u34-linux-x64.bin

安装到/usr/java目录下。

cd /usr
sudo mkdir java
cd java
sudo /home/ubuntu/jdk-6u34-linux-x64.bin

为当前安装的JDK创建别名是为了以后能够快速切换JDK版本(只需要改动软链接)

sudo ln -s /usr/java/jdk1.6.0_34 default

修改环境变量

sudo vim /etc/environment

注意应当往$CLASSPATH中加入.,以便支持java程序运行时从运行位置加载库

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/java/default/bin”
JAVA_HOME=”/usr/java/default”
CLASSPATH=”.:/usr/java/default/jre/lib/rt.jar:/usr/java/default/lib/dt.jar:/usr/java/default/lib/tools.jar”

需要退出重新登录一次以更新环境变量
检查是否正确

echo $JAVA_HOME
echo $CLASSPATH
java -version

如果安装Oracle JDK前ubuntu已经安装了Open JDK作为默认JDK,还得用update-alternatives修改默认JDK
update-alternatives和CentOS上的alternatives用法大体相同,就不再多说了

附:CentOS用JDK安装脚本

#!/bin/sh

TMP=/tmp
JDK_PACKAGE=jdk-6u32-linux-i586.bin
JDK_FOLDER=jdk1.6.0_32
JDK_LINK=/usr/bin/java
DEF_JDK_BASEDIR=/usr/java

usage() {
	cat <<EOS
Usage: $0 [OPTIONS]
  --jdk-basedir=        : JDK_BASEDIR
                          default: $DEF_JDK_BASEDIR
  --help                : What you're looking at.
EOS
}

parse_opts() {
	while [ $# -ne 0 ]; do
		opt=`echo $1 | cut -d'=' -f1`
		val=`echo $1 | cut -d'=' -f2`

		case "$opt" in
		--jdk-basedir) JDK_BASEDIR=$val;;
		--help)
			usage $0
			exit 1
			;;
		esac

		shift 1
	done
	if [ "$JDK_BASEDIR" = "" ]; then
		JDK_BASEDIR=$DEF_JDK_BASEDIR
	fi
}

parse_opts $*

# remove open-jdk
echo -n "NOTICE: open-jdk will be replaced by JDK 1.6.0(Oracle $JDK_FOLDER) as default JDK. Are you sure?[y/n]"
read YES_OR_NO
if [[ "$YES_OR_NO" != "y" && "$YES_OR_NO" != "Y" ]]; then
	exit 1
fi

#yum -y remove java-1.6.0-openjdk*

# download jdk
cd $TMP
wget -O $JDK_PACKAGE --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk-6u32-downloads-1594644.html;" http://download.oracle.com/otn-pub/java/jdk/6u32-b05/$JDK_PACKAGE
if [ ! -e ./$JDK_PACKAGE ]; then
	echo "ERROR: $JDK_PACKAGE is not existed."
	exit 1
fi

if [ ! -d $JDK_BASEDIR ]; then
	mkdir $JDK_BASEDIR
fi
cd $JDK_BASEDIR

# Clean job
rm -rf default
rm -rf $JDK_FOLDER

chmod +x $TMP/$JDK_PACKAGE
$TMP/$JDK_PACKAGE

if [ ! -d $JDK_FOLDER ]; then
	echo "Error: JDK version not matched."
	exit 1
fi
ln -s $JDK_FOLDER default

#export JAVA_HOME=$JDK_BASEDIR/default
#export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#export PATH=$PATH:$JAVA_HOME/bin

BAK_FILENAME=$(date +%Y%m%d%H%M%S)
cp -p /etc/profile /etc/profile.$BAK_FILENAME
cat >> /etc/profile <<EOS

export JAVA_HOME=$JDK_BASEDIR/default
export CLASSPATH=.:\$JAVA_HOME/jre/lib/rt.jar:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
export PATH=\$PATH:\$JAVA_HOME/bin
EOS

# change default java jdk from open-jdk to oracle-jdk
#if [ -e $JDK_LINK ]; then
alternatives --install $JDK_LINK java $JDK_BASEDIR/default/bin/java 19000
alternatives --set java $JDK_BASEDIR/default/bin/java
#fi

source ~/.bash_profile
source /etc/profile

java -version
if [ $? != 0 ]; then
	echo "Error: JDK setup not finished."
	exit 1
fi

rm -f $TMP/$JDK_PACKAGE
echo "JDK setup finished."
echo "You can excute \"alternatives --config java\" to change JDK version."
echo "Please logout and relogin."
2012年8月29日 | 归档于 技术
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: