2012年5月10日 星期四

在 Ubuntu 12.04 安裝及設定 JDK / JRE 6

蚊子學長在 "在 Ubuntu 12.04 LTS 安裝 Android SDK&NDK 開發環境" 這篇文章裡有提到在 Ubuntu 12.04 安裝 JDK 的方法,在這裡我再補充一些。


取得 JDK 6 的安裝檔,這裡示範是採用 64 位元的  jdk-6u32-linux-x64.bin 這個檔案,並將執行後得到的 jdk1.6.0_32 這個目錄搬移到 /usr/lib/jvm 路徑下。

接著在 /usr/lib/jvm 路徑下建立 jdk6-update-alternatives-script.sh 這個檔案,內容如下:

#!/bin/bash
# "Register JDK7 for update-alternatives Script in Debian Way"
# Based on 1. codeslinger's "install_java7_alternatives" from https://gist.github.com/445930
#          2. oracle-java6-plugin.deb postint script (for plugin)
# Modified by Cheng-Wei Chien, 2011-07-30
# ------------------------------------------------------------------------------
# Note:  This script works on x86 Debian GNU/Linux.
#        If you use other arch or distro, you may have to modify some parameters.
#
# Usage: 1. Extract jdk7 tar.gz to, for example, /usr/lib/jvm/java-7-oracle-1.7.0,
#           create a symbolic link /usr/lib/jvm/java-7-oracle to it.
#        2. Download java-7-oracle.jinfo from https://gist.github.com/1114515,
#           move it to /usr/lib/jvm/.java-7-oracle.jinfo, do some modification if necessary.
#        3. Run this script with sudo
#        4. sudo update-java-alternatives -s java-7-oracle
#        5. Have fun with jdk7!
#
# Trouble Shooting: If something goes wrong,
#        1. Uncomment "remove" commands and comment "install" commands.
#        2. Run this script again to rollback.
#        3. Fix what causes the problem, maybe typo or something else.
#        4. Comment "remove" commands and uncomment "install" commands
#        5. Run this script to update

# Force the shell to exit immediately if something goes wrong
set -e

# Follow the Debian's priority rule for java5 = 53, java6 = 63 .. I guess
priority=63

# Your jdk7 home, in this case, it's /usr/lib/jvm/java-7-oracle-1.7.0
# But I create a symbolic link /usr/lib/jvm/java-7-oracle to meet Debian's way
basedir=/usr/lib/jvm/jdk1.6.0_32

# Your man1 dir, you don't need to change this
mandir=$basedir/man/man1

# Variables from /usr/lib/jvm/.java-6-oracle.jinfo except HtmlConverter (not in jdk7)
jdk_var='appletviewer apt extcheck idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc'
jre_var='ControlPanel java java_vm javaws jcontrol keytool pack200 rmid rmiregistry unpack200 orbd servertool tnameserv jexec'
browser_var="xulrunner-addons firefox iceape iceweasel mozilla midbrowser xulrunner"

# Java plugin path, you don't need to change this
plugin=$basedir/jre/lib/amd64/libnpjp2.so

# First, gzip man1 files to meet the 1.gz form
if [ -e $mandir ]; then
  for i in $mandir/*.1.gz; do
    gzip $i
  done
fi

# Register jdk variables
for i in $jdk_var; do
  unset slave || true
  if [ -e $mandir/$i.1.gz ]; then
    slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
  fi
  #update-alternatives --remove $i $basedir/bin/$i
  update-alternatives --install /usr/bin/$i $i $basedir/bin/$i $priority $slave
done

# Register jre variables
for i in $jre_var; do
  unset slave || true
  if [ -e $mandir/$i.1.gz ]; then
    slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
  fi
  if [ $i = jexec ]; then
    #update-alternatives --remove jexec $basedir/jre/lib/jexec
    update-alternatives --install /usr/bin/jexec jexec $basedir/jre/lib/jexec $priority
  elif [ $i = java ]; then
    update-alternatives --install /usr/bin/java java $basedir/jre/bin/java $priority $slave --slave /usr/bin/policytool policytool $basedir/jre/bin/policytool
  else
    #update-alternatives --remove $i $basedir/jre/bin/$i
    update-alternatives --install /usr/bin/$i $i $basedir/jre/bin/$i $priority $slave
  fi
done

# Register plugins
for i in $browser_var; do
  if [ $i = xulrunner-addons ]; then
    browser=xulrunner-1.9
  else
    browser=$i
  fi
  #update-alternatives --remove $browser-javaplugin.so $plugin
  update-alternatives --install /usr/lib/$i/plugins/libjavaplugin.so $browser-javaplugin.so $plugin $priority
done

接著再建立 .jdk1.6.0_32.jinfo 這個檔案,請注意這個檔案會是一個隱藏檔,內容如下:

name=jdk1.6.0_32
alias=java-6-oracle
priority=63
section=non-free

jre ControlPanel /usr/lib/jvm/jdk1.6.0_32/jre/bin/ControlPanel
jre java /usr/lib/jvm/jdk1.6.0_32/jre/bin/java
jre java_vm /usr/lib/jvm/jdk1.6.0_32/jre/bin/java_vm
jre javaws /usr/lib/jvm/jdk1.6.0_32/jre/bin/javaws
jre jcontrol /usr/lib/jvm/jdk1.6.0_32/jre/bin/jcontrol
jre keytool /usr/lib/jvm/jdk1.6.0_32/jre/bin/keytool
jre policytool /usr/lib/jvm/jdk1.6.0_32/jre/bin/policytool
jre pack200 /usr/lib/jvm/jdk1.6.0_32/jre/bin/pack200
jre rmid /usr/lib/jvm/jdk1.6.0_32/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/jdk1.6.0_32/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/jdk1.6.0_32/jre/bin/unpack200
jre orbd /usr/lib/jvm/jdk1.6.0_32/jre/bin/orbd
jre servertool /usr/lib/jvm/jdk1.6.0_32/jre/bin/servertool
jre tnameserv /usr/lib/jvm/jdk1.6.0_32/jre/bin/tnameserv
jre jexec /usr/lib/jvm/jdk1.6.0_32/jre/lib/jexec
jdk appletviewer /usr/lib/jvm/jdk1.6.0_32/bin/appletviewer
jdk apt /usr/lib/jvm/jdk1.6.0_32/bin/apt
jdk extcheck /usr/lib/jvm/jdk1.6.0_32/bin/extcheck
jdk idlj /usr/lib/jvm/jdk1.6.0_32/bin/idlj
jdk jar /usr/lib/jvm/jdk1.6.0_32/bin/jar
jdk jarsigner /usr/lib/jvm/jdk1.6.0_32/bin/jarsigner
jdk javac /usr/lib/jvm/jdk1.6.0_32/bin/javac
jdk javadoc /usr/lib/jvm/jdk1.6.0_32/bin/javadoc
jdk javah /usr/lib/jvm/jdk1.6.0_32/bin/javah
jdk javap /usr/lib/jvm/jdk1.6.0_32/bin/javap
jdk jconsole /usr/lib/jvm/jdk1.6.0_32/bin/jconsole
jdk jdb /usr/lib/jvm/jdk1.6.0_32/bin/jdb
jdk jhat /usr/lib/jvm/jdk1.6.0_32/bin/jhat
jdk jinfo /usr/lib/jvm/jdk1.6.0_32/bin/jinfo
jdk jmap /usr/lib/jvm/jdk1.6.0_32/bin/jmap
jdk jps /usr/lib/jvm/jdk1.6.0_32/bin/jps
jdk jrunscript /usr/lib/jvm/jdk1.6.0_32/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/jdk1.6.0_32/bin/jsadebugd
jdk jstack /usr/lib/jvm/jdk1.6.0_32/bin/jstack
jdk jstat /usr/lib/jvm/jdk1.6.0_32/bin/jstat
jdk jstatd /usr/lib/jvm/jdk1.6.0_32/bin/jstatd
jdk native2ascii /usr/lib/jvm/jdk1.6.0_32/bin/native2ascii
jdk rmic /usr/lib/jvm/jdk1.6.0_32/bin/rmic
jdk schemagen /usr/lib/jvm/jdk1.6.0_32/bin/schemagen
jdk serialver /usr/lib/jvm/jdk1.6.0_32/bin/serialver
jdk wsgen /usr/lib/jvm/jdk1.6.0_32/bin/wsgen
jdk wsimport /usr/lib/jvm/jdk1.6.0_32/bin/wsimport
jdk xjc /usr/lib/jvm/jdk1.6.0_32/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin firefox-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin iceape-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin iceweasel-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin midbrowser-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so
plugin xulrunner-javaplugin.so /usr/lib/jvm/jdk1.6.0_32/jre/lib/amd64/libnpjp2.so

接著執行以下幾個指令:

$ chmod a+x jdk6-update-alternatives-script.sh
$ sudo ./jdk6-update-alternatives-script.sh
$ sudo update-java-alternatives -s jdk1.6.0_32

這麼一來,就將 JDK / JRE 6 整個搞定了!

上述的方法及腳本,我是參考 http://epsilony.net/mywiki/Ubuntu/JDK7Installation 這篇文章而來的。其中,腳本裡有些註解的說明我並沒有去修改它。


4 則留言:

  1. 這篇文章提到的方法,我在安裝時並沒遇上問題。但有大陸的網友留言給我說,他依法處理時發生 policytool 不能作為 slave 的狀況,必須將 policytool 當作 master alternative。

    若有網友也遇上類似狀況也請上來留言將遇到的狀況回應給我,謝謝。 ^^

    回覆刪除
  2. 我有同样的问题:
    update-alternatives: 错误: alternative policytool can't be slave of java: it is a master alternative.

    回覆刪除
  3. 我利用休息時間裝了 Ubuntu 12.04 的 Server 版,然後試著先裝了 Sun/Oracle JDK 6。

    但,後來在安裝 openjdk 時反而出現了以下的訊息:

    Setting up openjdk-6-jre (6b24-1.11.1-4ubuntu2) ...
    update-alternatives: error: alternative policytool can't be master: it is a slave of java

    所以,我猜想 OpenJDK 和 Sun/Oracle JDK 在處理 policytool 可能不一致。

    嗯……真不好搞。 XD

    回覆刪除
  4. 所以,如果不能將 policytool 裝成 slave,那麼在第一個腳本請改幾個地方。

    第一,請將

    jre_var='ControlPanel java java_vm javaws jcontrol keytool pack200 rmid rmiregistry unpack200 orbd servertool tnameserv jexec'
    browser_var="xulrunner-addons firefox iceape iceweasel mozilla midbrowser xulrunner"

    改成

    jre_var='ControlPanel java java_vm javaws jcontrol policytool keytool pack200 rmid rmiregistry unpack200 orbd servertool tnameserv jexec'
    browser_var="xulrunner-addons firefox iceape iceweasel mozilla midbrowser xulrunner"

    第二,在 # Register jre variables 裡拿掉以下這段:

    elif [ $i = java ]; then
    update-alternatives --install /usr/bin/java java $basedir/jre/bin/java $priority $slave --slave /usr/bin/policytool policytool $basedir/jre/bin/policytool

    回覆刪除