今天搗鼓Oracle的OS Watcher(簡稱osw) 的時候發現機器上的jdk不太好使,不能運行osw的oswg.jar。比較郁悶,看了一下 /usr/bin/java 是連接到 /etc/alternatives/java ,而 /etc/alternatives/java 是連接到 /usr/lib/jvm/jre-1.4.2-gcj/bin/java ,
1
[[email protected] bin]
# ll /usr/bin/java
2
lrwxrwxrwx 1 root root 22 Dec 17 2009 /usr/bin/java -> /etc/alternatives/java
3
[[email protected] bin]
# ll /etc/alternatives/java
4
lrwxrwxrwx 1 root root 35 Aug 23 14:52 /etc/alternatives/java -> /usr/lib/jvm/jre-1.4.2-gcj/bin/java
發現這個 alternatives 很眼生,於是 google 了一下,發現它是一個管理多版本軟件的軟件,於是借著升級jdk、jre的契機順便搗鼓一下這個alternatives 。
首先到Oracle的網站下載最新的 jdk 和 jre ,然後安裝。這個安裝比較惡心,裝在什麼地方也不說一聲,害我找了半天才發現裝在 /usr/java/jdk1.6.0_21/ 和 /usr/java/jre1.6.0_21/ 這兩個目錄中。
將jdk和jre的java注冊到alternatives中,順便也將jdk的javac注冊到alternatives中。
1
[[email protected] ~]
# alternatives --install /usr/bin/java java /usr/java/jre1.6.0_21/bin/java 400
2
[[email protected] ~]
# alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_21/bin/java 400
3
[[email protected] ~]
# alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_21/bin/javac 400
現在可以看看注冊的成果了:
01
[[email protected] ~]
# alternatives --config java
02
03
There are 3 programs
which
provide
'java'
.
04
05
Selection Command
06
-----------------------------------------------
07
*+ 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
08
2 /usr/java/jre1.6.0_21/bin/java
09
3 /usr/java/jdk1.6.0_21/bin/java
10
11
Enter to keep the current selection[+], or
type
selection number:
這裡輸入想要用的 java 就可以了,例如我選在了第2個。我們看看 /usr/bin/java的變化:
1
[[email protected] ~]
# ll /usr/bin/java
2
lrwxrwxrwx 1 root root 22 Dec 17 2009 /usr/bin/java -> /etc/alternatives/java
3
[[email protected] ~]
# ll /etc/alternatives/java
4
lrwxrwxrwx 1 root root 30 Aug 23 15:03 /etc/alternatives/java -> /usr/java/jre1.6.0_21/bin/java
可以看到 /usr/bin/java 的連接的地方沒有變,改變了的/etc/alternatives/java 的連接,這其實是一個策略模式的實現:
/usr/bin/java 的“調用”沒有變,還是連接到 /etc/alternatives/java,/etc/alternatives/java的連接卻被修改了,這個由 alternatives 管理。
通過linux的連接也可以簡單地實現這種“接口”和“具體實現”的分離,但是 alternatives 提供一個配置清單,簡單選一下就OK了,這為我們提供了很大的便利。
擴充一下,alternatives也可以管理我們自己的軟件。例如,我自己寫了個軟件叫myjava,我也想實現這種基於“策略模式”的版本管理,我可以這樣做:
1
alternatives --
install
/usr/bin/myjava myjava /usr/java/jdk1.6.0_21/bin/java 300
2
alternatives --
install
/usr/bin/myjava myjava /usr/java/jre1.6.0_21/bin/java 300
使用 alternatives 更換一下我的版本:
01
[[email protected] ~]
# ll /usr/bin/myjava
02
lrwxrwxrwx 1 root root 24 Aug 23 14:43 /usr/bin/myjava -> /etc/alternatives/myjava
03
[[email protected] ~]
# ll /etc/alternatives/myjava
04
lrwxrwxrwx 1 root root 30 Aug 23 14:46 /etc/alternatives/myjava -> /usr/java/jre1.6.0_21/bin/java
05
[[email protected] ~]
#
06
[[email protected] ~]
#
07
[[email protected] ~]
# alternatives --config myjava
08
09
There are 2 programs
which
provide
'myjava'
.
10
11
Selection Command
12
-----------------------------------------------
13
* 1 /usr/java/jdk1.6.0_21/bin/java
14
+ 2 /usr/java/jre1.6.0_21/bin/java
15
16
Enter to keep the current selection[+], or
type
selection number: 1
17
[[email protected] ~]
#
18
[[email protected] ~]
#
19
[[email protected] ~]
# ll /usr/bin/myjava
20
lrwxrwxrwx 1 root root 24 Aug 23 14:43 /usr/bin/myjava -> /etc/alternatives/myjava
21
[[email protected] ~]
# ll /etc/alternatives/myjava
22
lrwxrwxrwx 1 root root 30 Aug 23 15:15 /etc/alternatives/myjava -> /usr/java/jdk1.6.0_21/bin/java
是不是很方便 :-)