当前位置: 首页>>技术教程>>正文


如何使java默认为手动安装的JRE/JDK?

, , , ,

问题描述

我在我的机器上安装了Ubuntu 12.04 amd64,在以前版本的Ubuntu上它非常容易,现在有一个命令update-java-alternatives带有一个非常糟糕的手册页。

我只是将我的JDK解压缩到像/media/mydisk/jdk这样的挂载分区上,我如何强制使用该JDK而不是Ubuntu存储库中的JDK?

这个update-java-alternatives背后的逻辑是什么?

最佳解决方法

Note: update-java-alternatives won’t work with manual installations. In any case, it doesn’t look like it has any special abilities which update-alternatives doesn’t have, except convenience.

作为@ AnwarShah的命令行方法的图形替代方案,请考虑使用Gnome Alternatives(sudo apt-get install galternatives):

  • 启动GAlternatives,选择每个java选项,将其更改为手动,然后添加替代方案(此处的示例是我的主目录中安装的IBM JRE):

    java,jdk,alternative,jre,ubuntu

    java,jdk,alternative,jre,ubuntu

    java,jdk,alternative,jre,ubuntu

次佳解决方法

您可以使用sudo update-alternatives执行此操作:

# Adding a new alternative for "java".
sudo update-alternatives --install /usr/bin/java java /media/mydisk/jdk/bin/java 1

# Setting the new alternative as default for "java".
sudo update-alternatives --config java

您还必须为其他jdk组件执行此操作。这些组件是appletviewerextcheckidljjarjarsignerjavajavacjavadocjavahjavapjavawsjconsolejdbjexecjhatjinfojmapjpsjrunscriptjsadebugdjstackjstatjstatdkeytoolmozilla-javaplugin.sonative2asciiorbdpack200policytoolrmicrmidrmiregistryschemagenserialverservertooltnameservunpack200wsgenwsimportxjc

有关详细信息,请参阅update-alternativesman update-alternatives的手册页。

命令update-java-alternatives设置java组件的替代品,它似乎使用update-alternatives

编辑:上面的答案针对Java 6。

  • 对于Java 7,您必须将jcmd添加到Java 6列表中。

  • 对于Java 8,您必须将jcmdjjsjdeps添加到Java 6列表中。

  • 对于Java 9,您必须将jcmdjjsjdepsjshell添加到Java 6列表中。

第三种解决方法

我实际上使用这些命令来安装jdk1.6.0_25。 (查看下面的链接,了解更新的完整update-alternatives脚本)

sudo update-alternatives --install /usr/bin/java java /media/mydisk/jdk/bin/java 100
sudo update-alternatives --install /usr/bin/javac javac /media/mydisk/jdk/javac 100
sudo update-alternatives --install /usr/bin/java_vm java_vm /media/mydisk/jdk/bin/java_vm 100
sudo update-alternatives --install /usr/bin/jcontrol jcontrol /media/mydisk/jdk/bin/jcontrol 100
sudo update-alternatives --install /usr/bin/jexec jexec /media/mydisk/jdk/jre/lib/jexec 100
sudo update-alternatives --install /usr/bin/jexec jexec /media/mydisk/jdk/jre/lib/jexec 100 --slave /usr/share/binfmts/jar jexec-binfmt media/mydisk/jdk/jre/lib/javaws

然后您可以使用这些命令设置为默认值。最有可能的是,这些都不是必需的。

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config java_vm
sudo update-alternatives --config jcontrol
sudo update-alternatives --config jexec

您可以使用命令man update-alternatives获得更多帮助,或者您可以看到manual in your browser here

手册页中描述的update-java-alternatives的用途是

update-java-alternatives updates all alternatives belonging to one runtime or development kit for the Java language. A package does provide these information of it’s alternatives in /usr/lib/jvm/.<jname>.jinfo.

所有的jdk工具都是

appletviewer 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

希望这会帮助你。


Update

我使用此脚本来提取下载的jdk,并使用update-alternatives完全设置它。修改jdk文件名和JAVA_DIR以满足您的需要。 JAVA_DIR是提取的jdk文件夹的路径。

#!/bin/sh

JAVA_DIR=/usr/lib/jvm/jdk1.8.0_65

# extract a jdk file named `jdk-8u65-linux-x64.tar.gz` from current directory to `/usr/lib/jvm`
sudo tar xvf jdk-8u65-linux-x64.tar.gz -C /usr/lib/jvm/

# update alternative links
sudo update-alternatives --quiet --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so $JAVA_DIR/jre/lib/amd64/libnpjp2.so 100
sudo update-alternatives --quiet --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so $JAVA_DIR/jre/lib/amd64/libnpjp2.so 100
sudo update-alternatives --quiet --install /usr/bin/appletviewer appletviewer $JAVA_DIR/bin/appletviewer 100 --slave /usr/share/man/man1/appletviewer.1 appletviewer.1 $JAVA_DIR/man/man1/appletviewer.1
sudo update-alternatives --quiet --install /usr/bin/apt apt $JAVA_DIR/bin/apt 100 --slave /usr/share/man/man1/apt.1 apt.1 $JAVA_DIR/man/man1/apt.1
sudo update-alternatives --quiet --install /usr/bin/extcheck extcheck $JAVA_DIR/bin/extcheck 100 --slave /usr/share/man/man1/extcheck.1 extcheck.1 $JAVA_DIR/man/man1/extcheck.1
sudo update-alternatives --quiet --install /usr/bin/idlj idlj $JAVA_DIR/bin/idlj 100 --slave /usr/share/man/man1/idlj.1 idlj.1 $JAVA_DIR/man/man1/idlj.1
sudo update-alternatives --quiet --install /usr/bin/jar jar $JAVA_DIR/bin/jar 100 --slave /usr/share/man/man1/jar.1 jar.1 $JAVA_DIR/man/man1/jar.1
sudo update-alternatives --quiet --install /usr/bin/jarsigner jarsigner $JAVA_DIR/bin/jarsigner 100 --slave /usr/share/man/man1/jarsigner.1 jarsigner.1 $JAVA_DIR/man/man1/jarsigner.1
sudo update-alternatives --quiet --install /usr/bin/javac javac $JAVA_DIR/bin/javac 100 --slave /usr/share/man/man1/javac.1 javac.1 $JAVA_DIR/man/man1/javac.1
sudo update-alternatives --quiet --install /usr/bin/javadoc javadoc $JAVA_DIR/bin/javadoc 100 --slave /usr/share/man/man1/javadoc.1 javadoc.1 $JAVA_DIR/man/man1/javadoc.1
sudo update-alternatives --quiet --install /usr/bin/javah javah $JAVA_DIR/bin/javah 100 --slave /usr/share/man/man1/javah.1 javah.1 $JAVA_DIR/man/man1/javah.1
sudo update-alternatives --quiet --install /usr/bin/javap javap $JAVA_DIR/bin/javap 100 --slave /usr/share/man/man1/javap.1 javap.1 $JAVA_DIR/man/man1/javap.1
sudo update-alternatives --quiet --install /usr/bin/jconsole jconsole $JAVA_DIR/bin/jconsole 100 --slave /usr/share/man/man1/jconsole.1 jconsole.1 $JAVA_DIR/man/man1/jconsole.1
sudo update-alternatives --quiet --install /usr/bin/jdb jdb $JAVA_DIR/bin/jdb 100 --slave /usr/share/man/man1/jdb.1 jdb.1 $JAVA_DIR/man/man1/jdb.1
sudo update-alternatives --quiet --install /usr/bin/jhat jhat $JAVA_DIR/bin/jhat 100 --slave /usr/share/man/man1/jhat.1 jhat.1 $JAVA_DIR/man/man1/jhat.1
sudo update-alternatives --quiet --install /usr/bin/jinfo jinfo $JAVA_DIR/bin/jinfo 100 --slave /usr/share/man/man1/jinfo.1 jinfo.1 $JAVA_DIR/man/man1/jinfo.1
sudo update-alternatives --quiet --install /usr/bin/jmap jmap $JAVA_DIR/bin/jmap 100 --slave /usr/share/man/man1/jmap.1 jmap.1 $JAVA_DIR/man/man1/jmap.1
sudo update-alternatives --quiet --install /usr/bin/jps jps $JAVA_DIR/bin/jps 100 --slave /usr/share/man/man1/jps.1 jps.1 $JAVA_DIR/man/man1/jps.1
sudo update-alternatives --quiet --install /usr/bin/jrunscript jrunscript $JAVA_DIR/bin/jrunscript 100 --slave /usr/share/man/man1/jrunscript.1 jrunscript.1 $JAVA_DIR/man/man1/jrunscript.1
sudo update-alternatives --quiet --install /usr/bin/jsadebugd jsadebugd $JAVA_DIR/bin/jsadebugd 100 --slave /usr/share/man/man1/jsadebugd.1 jsadebugd.1 $JAVA_DIR/man/man1/jsadebugd.1
sudo update-alternatives --quiet --install /usr/bin/jstack jstack $JAVA_DIR/bin/jstack 100 --slave /usr/share/man/man1/jstack.1 jstack.1 $JAVA_DIR/man/man1/jstack.1
sudo update-alternatives --quiet --install /usr/bin/jstat jstat $JAVA_DIR/bin/jstat 100 --slave /usr/share/man/man1/jstat.1 jstat.1 $JAVA_DIR/man/man1/jstat.1
sudo update-alternatives --quiet --install /usr/bin/jstatd jstatd $JAVA_DIR/bin/jstatd 100 --slave /usr/share/man/man1/jstatd.1 jstatd.1 $JAVA_DIR/man/man1/jstatd.1
sudo update-alternatives --quiet --install /usr/bin/native2ascii native2ascii $JAVA_DIR/bin/native2ascii 100 --slave /usr/share/man/man1/native2ascii.1 native2ascii.1 $JAVA_DIR/man/man1/native2ascii.1
sudo update-alternatives --quiet --install /usr/bin/rmic rmic $JAVA_DIR/bin/rmic 100 --slave /usr/share/man/man1/rmic.1 rmic.1 $JAVA_DIR/man/man1/rmic.1
sudo update-alternatives --quiet --install /usr/bin/schemagen schemagen $JAVA_DIR/bin/schemagen 100 --slave /usr/share/man/man1/schemagen.1 schemagen.1 $JAVA_DIR/man/man1/schemagen.1
sudo update-alternatives --quiet --install /usr/bin/serialver serialver $JAVA_DIR/bin/serialver 100 --slave /usr/share/man/man1/serialver.1 serialver.1 $JAVA_DIR/man/man1/serialver.1
sudo update-alternatives --quiet --install /usr/bin/wsgen wsgen $JAVA_DIR/bin/wsgen 100 --slave /usr/share/man/man1/wsgen.1 wsgen.1 $JAVA_DIR/man/man1/wsgen.1
sudo update-alternatives --quiet --install /usr/bin/wsimport wsimport $JAVA_DIR/bin/wsimport 100 --slave /usr/share/man/man1/wsimport.1 wsimport.1 $JAVA_DIR/man/man1/wsimport.1
sudo update-alternatives --quiet --install /usr/bin/xjc xjc $JAVA_DIR/bin/xjc 100 --slave /usr/share/man/man1/xjc.1 xjc.1 $JAVA_DIR/man/man1/xjc.1
sudo update-alternatives --quiet --install /usr/bin/java-rmi.cgi java-rmi.cgi $JAVA_DIR/bin/java-rmi.cgi 100
sudo update-alternatives --quiet --install /usr/bin/ControlPanel ControlPanel $JAVA_DIR/jre/bin/ControlPanel 100
sudo update-alternatives --quiet --install /usr/bin/java java $JAVA_DIR/jre/bin/java 100
sudo update-alternatives --quiet --install /usr/bin/java_vm java_vm $JAVA_DIR/jre/bin/java_vm 100
sudo update-alternatives --quiet --install /usr/bin/javaws javaws $JAVA_DIR/jre/bin/javaws 100
sudo update-alternatives --quiet --install /usr/bin/jcontrol jcontrol $JAVA_DIR/jre/bin/jcontrol 100
sudo update-alternatives --quiet --install /usr/bin/keytool keytool $JAVA_DIR/jre/bin/keytool 100
sudo update-alternatives --quiet --install /usr/bin/pack200 pack200 $JAVA_DIR/jre/bin/pack200 100
sudo update-alternatives --quiet --install /usr/bin/policytool policytool $JAVA_DIR/jre/bin/policytool 100
sudo update-alternatives --quiet --install /usr/bin/rmid rmid $JAVA_DIR/jre/bin/rmid 100
sudo update-alternatives --quiet --install /usr/bin/rmiregistry rmiregistry $JAVA_DIR/jre/bin/rmiregistry 100
sudo update-alternatives --quiet --install /usr/bin/unpack200 unpack200 $JAVA_DIR/jre/bin/unpack200 100
sudo update-alternatives --quiet --install /usr/bin/orbd orbd $JAVA_DIR/jre/bin/orbd 100
sudo update-alternatives --quiet --install /usr/bin/servertool servertool $JAVA_DIR/jre/bin/servertool 100
sudo update-alternatives --quiet --install /usr/bin/tnameserv tnameserv $JAVA_DIR/jre/bin/tnameserv 100
sudo update-alternatives --quiet --install /usr/bin/jexec jexec $JAVA_DIR/jre/lib/jexec 100

第四种方法

您也可以使用此bash脚本:

#!/bin/bash

export JDK_INSTALL=/opt/jdk1.8.0_112
#export JDK_INSTALL=/opt/jdk1.7.0_25
#export JDK_INSTALL=/opt/jdk1.6.0_45


#JDK 6 and above

sudo update-alternatives --install /usr/bin/appletviewer appletviewer $JDK_INSTALL/bin/appletviewer 1
sudo update-alternatives --install /usr/bin/extcheck extcheck $JDK_INSTALL/bin/extcheck 1
sudo update-alternatives --install /usr/bin/idlj idlj $JDK_INSTALL/bin/idlj 1
sudo update-alternatives --install /usr/bin/jar jar $JDK_INSTALL/bin/jar 1
sudo update-alternatives --install /usr/bin/jarsigner jarsigner $JDK_INSTALL/bin/jarsigner 1
sudo update-alternatives --install /usr/bin/java java $JDK_INSTALL/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac $JDK_INSTALL/bin/javac 1
sudo update-alternatives --install /usr/bin/javadoc javadoc $JDK_INSTALL/bin/javadoc 1
sudo update-alternatives --install /usr/bin/javah javah $JDK_INSTALL/bin/javah 1
sudo update-alternatives --install /usr/bin/javap javap $JDK_INSTALL/bin/javap 1
sudo update-alternatives --install /usr/bin/javaws javaws $JDK_INSTALL/bin/javaws 1
sudo update-alternatives --install /usr/bin/jconsole jconsole $JDK_INSTALL/bin/jconsole 1
sudo update-alternatives --install /usr/bin/jdb jdb $JDK_INSTALL/bin/jdb 1
sudo update-alternatives --install /usr/bin/jhat jhat $JDK_INSTALL/bin/jhat 1
sudo update-alternatives --install /usr/bin/jinfo jinfo $JDK_INSTALL/bin/jinfo 1
sudo update-alternatives --install /usr/bin/jmap jmap $JDK_INSTALL/bin/jmap 1
sudo update-alternatives --install /usr/bin/jps jps $JDK_INSTALL/bin/jps 1
sudo update-alternatives --install /usr/bin/jrunscript jrunscript $JDK_INSTALL/bin/jrunscript 1
sudo update-alternatives --install /usr/bin/jsadebugd jsadebugd $JDK_INSTALL/bin/jsadebugd 1
sudo update-alternatives --install /usr/bin/jstack jstack $JDK_INSTALL/bin/jstack 1
sudo update-alternatives --install /usr/bin/jstat jstat $JDK_INSTALL/bin/jstat 1
sudo update-alternatives --install /usr/bin/jstatd jstatd $JDK_INSTALL/bin/jstatd 1
sudo update-alternatives --install /usr/bin/keytool keytool $JDK_INSTALL/bin/keytool 1
sudo update-alternatives --install /usr/bin/native2ascii native2ascii $JDK_INSTALL/bin/native2ascii 1
sudo update-alternatives --install /usr/bin/orbd orbd $JDK_INSTALL/bin/orbd 1
sudo update-alternatives --install /usr/bin/pack200 pack200 $JDK_INSTALL/bin/pack200 1
sudo update-alternatives --install /usr/bin/policytool policytool $JDK_INSTALL/bin/policytool 1
sudo update-alternatives --install /usr/bin/rmic rmic $JDK_INSTALL/bin/rmic 1
sudo update-alternatives --install /usr/bin/rmid rmid $JDK_INSTALL/bin/rmid 1
sudo update-alternatives --install /usr/bin/rmiregistry rmiregistry $JDK_INSTALL/bin/rmiregistry 1
sudo update-alternatives --install /usr/bin/schemagen schemagen $JDK_INSTALL/bin/schemagen 1
sudo update-alternatives --install /usr/bin/serialver serialver $JDK_INSTALL/bin/serialver 1
sudo update-alternatives --install /usr/bin/servertool servertool $JDK_INSTALL/bin/servertool 1
sudo update-alternatives --install /usr/bin/tnameserv tnameserv $JDK_INSTALL/bin/tnameserv 1
sudo update-alternatives --install /usr/bin/unpack200 unpack200 $JDK_INSTALL/bin/unpack200 1
sudo update-alternatives --install /usr/bin/wsgen wsgen $JDK_INSTALL/bin/wsgen 1
sudo update-alternatives --install /usr/bin/wsimport wsimport $JDK_INSTALL/bin/wsimport 1
sudo update-alternatives --install /usr/bin/xjc xjc $JDK_INSTALL/bin/xjc 1

sudo update-alternatives --set appletviewer $JDK_INSTALL/bin/appletviewer
sudo update-alternatives --set extcheck $JDK_INSTALL/bin/extcheck
sudo update-alternatives --set idlj $JDK_INSTALL/bin/idlj
sudo update-alternatives --set jar $JDK_INSTALL/bin/jar
sudo update-alternatives --set jarsigner $JDK_INSTALL/bin/jarsigner
sudo update-alternatives --set java $JDK_INSTALL/bin/java
sudo update-alternatives --set javac $JDK_INSTALL/bin/javac
sudo update-alternatives --set javadoc $JDK_INSTALL/bin/javadoc
sudo update-alternatives --set javah $JDK_INSTALL/bin/javah
sudo update-alternatives --set javap $JDK_INSTALL/bin/javap
sudo update-alternatives --set javaws $JDK_INSTALL/bin/javaws
sudo update-alternatives --set jconsole $JDK_INSTALL/bin/jconsole
sudo update-alternatives --set jdb $JDK_INSTALL/bin/jdb
sudo update-alternatives --set jhat $JDK_INSTALL/bin/jhat
sudo update-alternatives --set jinfo $JDK_INSTALL/bin/jinfo
sudo update-alternatives --set jmap $JDK_INSTALL/bin/jmap
sudo update-alternatives --set jps $JDK_INSTALL/bin/jps
sudo update-alternatives --set jrunscript $JDK_INSTALL/bin/jrunscript
sudo update-alternatives --set jsadebugd $JDK_INSTALL/bin/jsadebugd
sudo update-alternatives --set jstack $JDK_INSTALL/bin/jstack
sudo update-alternatives --set jstat $JDK_INSTALL/bin/jstat
sudo update-alternatives --set jstatd $JDK_INSTALL/bin/jstatd
sudo update-alternatives --set keytool $JDK_INSTALL/bin/keytool
sudo update-alternatives --set native2ascii $JDK_INSTALL/bin/native2ascii
sudo update-alternatives --set orbd $JDK_INSTALL/bin/orbd
sudo update-alternatives --set pack200 $JDK_INSTALL/bin/pack200
sudo update-alternatives --set policytool $JDK_INSTALL/bin/policytool
sudo update-alternatives --set rmic $JDK_INSTALL/bin/rmic
sudo update-alternatives --set rmid $JDK_INSTALL/bin/rmid
sudo update-alternatives --set rmiregistry $JDK_INSTALL/bin/rmiregistry
sudo update-alternatives --set schemagen $JDK_INSTALL/bin/schemagen
sudo update-alternatives --set serialver $JDK_INSTALL/bin/serialver
sudo update-alternatives --set servertool $JDK_INSTALL/bin/servertool
sudo update-alternatives --set tnameserv $JDK_INSTALL/bin/tnameserv
sudo update-alternatives --set unpack200 $JDK_INSTALL/bin/unpack200
sudo update-alternatives --set wsgen $JDK_INSTALL/bin/wsgen
sudo update-alternatives --set wsimport $JDK_INSTALL/bin/wsimport
sudo update-alternatives --set xjc $JDK_INSTALL/bin/xjc


#JDK 7 and above

sudo update-alternatives --install /usr/bin/jcmd jcmd $JDK_INSTALL/bin/jcmd 1

sudo update-alternatives --set jcmd $JDK_INSTALL/bin/jcmd


#JDK 8 only

sudo update-alternatives --install /usr/bin/jjs jjs $JDK_INSTALL/bin/jjs 1
sudo update-alternatives --install /usr/bin/jdeps jdeps $JDK_INSTALL/bin/jdeps 1

sudo update-alternatives --set jjs $JDK_INSTALL/bin/jjs
sudo update-alternatives --set jdeps $JDK_INSTALL/bin/jdeps

第五种方法

对我来说,大多数答案都太复杂了。

最初,Oracle决定能够基于设置一些环境变量来安装多个Java版本。

它很容易,但对于那些不了解这些变量的人来说太复杂了,有人发明了”update-java-alternatives”。

事实证明”update-java-alternatives”很简单,当你为所有东西配置时,你只需要执行这个程序,然后选择你想要的版本。

问题是这个解决方案太复杂了,如果你必须自己配置(你必须为java的每个命令配置它)。

最好的答案是回归基础。

在您的.bash_profile(适用于您的用户)或/etc/profile(适用于每个用户)中设置以下变量:

JAVA_HOME=<The home of your new java distribution>

PATH=<The bin directory of your new java distribution>:$PATH

在我的情况下,这更容易……我已经在/etc/profile.d中有一个文件,其中包含以下内容(只是将其更新为新的目录结构):

export J2SDKDIR=/usr/lib/jvm/jdk1.8.0_121 
export J2REDIR=/usr/lib/jvm/jdk1.8.0_121/jre
export PATH=/usr/lib/jvm/jdk1.8.0_121/bin:/usr/lib/jvm/jdk1.8.0_121/db/bin:/usr/lib/jvm/jdk1.8.0_121/jre/bin:$PATH
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_121
export DERBY_HOME=/usr/lib/jvm/jdk1.8.0_121/db

就这样!!!

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/1956.html,未经允许,请勿转载。