2013年9月20日 星期五

在 CentOS 6.4 安裝 Oracle JDK 6

安裝完 CentOS 6.4 後,我發現 CentOS 的套件庫裡也沒有 Oracle JDK 的套件,都是收錄了 OpenJDK。於是,我打算依照之前的作法自行安裝 Oracle JDK 6。在動手安裝之前,我還是爬了爬文,找了幾篇參考的文章:

  • HOW TO INSTALL AND SETUP ORACLE JAVA JDK IN CENTOS 6
  • Install Sun/Oracle Java JDK/JRE 6u45 on Fedora 19/18, CentOS/RHEL 6.4/5.9
  • Install Oracle Java JDK/JRE 7u25 on Fedora 19/18, CentOS/RHEL 6.4/5.9
  • 安裝的步驟並不麻煩,首先自 Oracle 的網站下載了 Oracle JDK 6 的 jdk-6u45-linux-x64-rpm.bin 這個檔案,然後執行它:

    $ sudo ./jdk-6u45-linux-x64-rpm.bin

    接著,使用 alternatives –install 指令安裝 Oracle JDK:

    $ alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_45/jre/bin/java 20000
    $ sudo alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.6.0_45/jre/bin/javaws 20000
    $ sudo alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jdk1.6.0_45/jre/lib/i386/libnpjp2.so 20000
    $ sudo alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so 20000
    $ sudo alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_45/bin/javac 20000
    $ sudo alternatives --install /usr/bin/javac javah /usr/java/jdk1.6.0_45/bin/javah 20000
    $ sudo alternatives --install /usr/bin/jar jar /usr/java/jdk1.6.0_45/bin/jar 20000

    而根據爬文找到的文章,應該也可以使用像這樣的指令設定 JDK,如:

    $ sudo alternatives --set /usr/bin/java java /usr/java/jdk1.6.0_45/jre/bin/java

    由於我在 CentOS 6.4 裡也安裝了 java-1.7.0-openjdk-devel,所以我執行了下面這個指令選擇 Oracle JDK 作為預設的 Java:

    $ sudo alternatives --config java

    但,當我要修改 jar 和 javah 時,卻發現 /usr/bin/jar/usr/bin/javah 消失了。

    最後,我乾脆將 java-1.7.0-openjdk-devel 移除,然後手動建立 /usr/bin/java 等相關連結。 (悶)

    $ ls -l /usr/bin/ja*
    lrwxrwxrwx. 1 root root 21 Sep 16 12:51 /usr/bin/jar -> /etc/alternatives/jar
    lrwxrwxrwx. 1 root root 27 Sep 16 12:54 /usr/bin/jarsigner -> /etc/alternatives/jarsigner
    lrwxrwxrwx. 1 root root 22 Sep 16 12:40 /usr/bin/java -> /etc/alternatives/java
    lrwxrwxrwx. 1 root root 23 Sep 16 12:40 /usr/bin/javac -> /etc/alternatives/javac
    lrwxrwxrwx. 1 root root 25 Sep 16 12:55 /usr/bin/javadoc -> /etc/alternatives/javadoc
    lrwxrwxrwx. 1 root root 23 Sep 16 12:41 /usr/bin/javah -> /etc/alternatives/javah
    lrwxrwxrwx. 1 root root 23 Sep 16 12:55 /usr/bin/javap -> /etc/alternatives/javap
    lrwxrwxrwx. 1 root root 24 Sep 16 12:37 /usr/bin/javaws -> /etc/alternatives/javaws

    其實,/usr/bin/java 只是一個指向 /etc/alternatives/java 的連結。而 /etc/alternatives/java 才是真正指向 Java 的連結。

    $ ls -l /etc/alternatives/ja*
    lrwxrwxrwx. 1 root root 29 Sep 16 12:51 /etc/alternatives/jar -> /usr/java/jdk1.6.0_45/bin/jar
    lrwxrwxrwx. 1 root root 35 Sep 16 12:54 /etc/alternatives/jarsigner -> /usr/java/jdk1.6.0_45/bin/jarsigner
    lrwxrwxrwx. 1 root root 34 Sep 16 12:40 /etc/alternatives/java -> /usr/java/jdk1.6.0_45/jre/bin/java
    lrwxrwxrwx. 1 root root 31 Sep 16 12:40 /etc/alternatives/javac -> /usr/java/jdk1.6.0_45/bin/javac
    lrwxrwxrwx. 1 root root 33 Sep 16 12:55 /etc/alternatives/javadoc -> /usr/java/jdk1.6.0_45/bin/javadoc
    lrwxrwxrwx. 1 root root 31 Sep 16 12:41 /etc/alternatives/javah -> /usr/java/jdk1.6.0_45/bin/javah
    lrwxrwxrwx. 1 root root 31 Sep 16 12:55 /etc/alternatives/javap -> /usr/java/jdk1.6.0_45/bin/javap
    lrwxrwxrwx. 1 root root 36 Sep 16 12:37 /etc/alternatives/javaws -> /usr/java/jdk1.6.0_45/jre/bin/javaws

    張貼留言