OpenSUSE下如何安装JDK

2016-09-27 20:51:38 3183

openSUSE自带了OpenJDK,安装Oracle JDK后,需要使用alternatives切换系统默认使用的JDK版本。

1)安装JDK:

linuxidc@linux-linuxidc:~/下载> sudo zypper install jdk-8u66-linux-x64.rpm -y  
...
/var/tmp/rpm-tmp.DfGrcZ: line 638: /usr/sbin/alternatives: No such file or directory
/var/tmp/rpm-tmp.DfGrcZ: line 650: /usr/sbin/alternatives: No such file or directory
warning: %post(jdk1.8.0_66-2000:1.8.0_66-fcs.x86_64) scriptlet failed, exit status 127

有如上的报错,“/usr/sbin/alternatives”这个文件找不到。openSUSE相应的文件名是update-alternatives,尝试做了软连接,会报新的错误:

update-alternatives: error: alternative javaws can't be slave of java: it is a slave of javaplugin
update-alternatives: error: alternative javaws can't be slave of javac: it is a slave of javaplugin
warning: %post(jdk1.8.0_66-2000:1.8.0_66-fcs.x86_64) scriptlet failed, exit status 2

看来openSUSE和Oracle JDK不是很友好啊,需要手动完成后续的必要步骤(从网上找的,可能不是安装脚本的全部后续操作)。

2)用alternatives声明jdk版本并设为默认:

linuxidc@linux-linuxidc:~/下载> sudo /usr/sbin/update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.8.0_66/bin/java" 40
linuxidc@linux-linuxidc:~/下载> sudo /usr/sbin/update-alternatives --install "/usr/lib64/browser-plugins/javaplugin.so" "javaplugin" "/usr/java/jdk1.8.0_66/jre/lib/amd64/libnpjp2.so" 40

——其中红色部分根据操作系统及指令集不同会有不同。

linuxidc@linux-linuxidc:~/下载> sudo /usr/sbin/update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                      Priority  Status
------------------------------------------------------------
* 0            /usr/lib64/jvm/jre-1.8.0-openjdk/bin/java  18040    auto mode
  1            /usr/java/jdk1.8.0_66/bin/java              40        manual mode
  2            /usr/lib64/jvm/jre-1.7.0-openjdk/bin/java  17147    manual mode
  3            /usr/lib64/jvm/jre-1.8.0-openjdk/bin/java  18040    manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/java/jdk1.8.0_66/bin/java to provide /usr/bin/java (java) in manual mode
linuxidc@linux-linuxidc:~/下载> sudo /usr/sbin/update-alternatives --config javaplugin
There are 3 choices for the alternative javaplugin (providing /usr/lib64/browser-plugins/javaplugin.so).

  Selection    Path                                                      Priority  Status
------------------------------------------------------------
* 0            /usr/lib64/java-1_8_0-openjdk-plugin/lib/IcedTeaPlugin.so  18020    auto mode
  1            /usr/java/jdk1.8.0_66/jre/lib/amd64/libnpjp2.so            40        manual mode
  2            /usr/lib64/java-1_7_0-openjdk-plugin/lib/IcedTeaPlugin.so  17147    manual mode
  3            /usr/lib64/java-1_8_0-openjdk-plugin/lib/IcedTeaPlugin.so  18020    manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/java/jdk1.8.0_66/jre/lib/amd64/libnpjp2.so to provide /usr/lib64/browser-plugins/javaplugin.so (javaplugin) in manual mode

3)查看JDK版本:
linuxidc@linux-linuxidc:~/下载> java -version
java version "1.8.0_66"

4)修改JAVA_HOME:

但是此时JAVA_HOME尚不正确:

linuxidc@linux-linuxidc:~/下载> echo $JAVA_HOME
/usr/lib64/jvm/jre

修改/etc/profile文件,在文件尾追加:

export JAVA_HOME=/usr/java/jdk1.8.0_66
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

退出,重新登陆后:

linuxidc@linux-linuxidc:~> echo $JAVA_HOME
/usr/java/jdk1.8.0_66



提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: