如何将Java默认设置为手动安装的JRE / JDK?


54

我的机器上安装了Ubuntu 12.04 amd64,在先前版本的Ubuntu上这非常容易,现在有了此命令,update-java-alternatives其中的手册页确实很糟糕。

我只是将我的JDK解包到一个已安装的分区上,例如/media/mydisk/jdk,该如何强制使用该JDK而不是Ubuntu存储库中的JDK?

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


Answers:


30

注意:update-java-alternatives不适用于手动安装。在任何情况下update-alternatives,除了方便之外,它看起来都不具有任何其他特殊功能。

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

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

    在此处输入图片说明

    在此处输入图片说明

    在此处输入图片说明


41

您可以使用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,您必须添加jcmdjjs并将jdeps其添加到Java 6列表中。
  • 对于Java 9,你必须添加jcmdjjsjdepsjshell在Java 6的列表。

似乎很多答案都提到需要安装的Java文件的不同子集。例如,@ air-dex的答案提到了一大堆。有人可以通过更新替代呼叫告诉我我需要安装什么吗?在哪里可以找到有关此信息?
托马斯,2015年

@thomas通过update-alternatives安装我在这里提到的所有替代方案。我的列表包含JDK的所有Java命令。
air-dex 2015年

1
还看一下这些JDK组件:orbd,pack200,您可能也想更改它们
JaroslavZáruba16年

24

我实际上使用这些命令进行安装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也可以在此处的浏览器中查看手册

update-java-alternatives手册页中所述的目的是

update-java-alternatives更新属于Java语言的一种运行时或开发套件的所有替代项。套件确实提供了这些替代品的这些信息/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

希望这会帮助你。


更新资料

我使用此脚本来提取下载的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

javac应该在/ media / mydisk / jdk / bin / javac下面找到
Jonathan Neufeld

3

您还可以使用以下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

这看起来像是一个循环更干净:/
cricket_007

3

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

最初,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

就这样!!!


1

它并不像其他答案那么复杂。您必须将手动安装的Java添加到替代列表,然后切换到is :(将jdk1.7.0_79替换为您的版本)

sudo /usr/sbin/alternatives --install /usr/java/jdk1.7.0_79/bin/java 20000
sudo /usr/sbin/alternatives --config java

1
您缺少--install的两个参数,它应该是'/ usr / sbin / alternatives --install / usr / bin / java java /usr/java/jdk1.7.0_79/bin/java 20000'。
gaoithe 2015年

1

update-java-alternatives实际上非常不错。因此,要使其与我的手动安装一起使用,请执行以下操作:将jdk1.8.0_45复制到/ usr / lib / jvm

cp .java-1.7.0-openjdk-amd64.jinfo .java-1.8.0-u45-amd64.jinfo
ln -s jdk1.8.0_45 java-1.8.0-u45-amd64

然后我用vim进行搜索/替换:

vim .java-1.8.0-u45-amd64.jinfo
:%s/java-7-openjdk-amd64/java-1.8.0-u45-amd64
:wq

也将优先级递减1

:sigh:update-java-alternatives不执行安装。但是至少我们有一个文件可以使用

VirtualBox:/usr/lib/jvm$ cat .java-1.8.0-u45-amd64.jinfo  | perl -e 'while (<>) { @line = split(/\s+/); $filename = $line[1]; $abspath = $line[2]; $abspath =~ /(.*jdk[^\/]+)/; $manpath = $1 . "/man/man1/"; $manfile = "$manpath$filename.1"; if (-f $manfile) { system("sudo gzip $manfile"); } system("sudo update-alternatives --install /usr/bin/$filename $filename $abspath 1070 --slave /usr/share/man/man1/$filename.1.gz $filename.1.gz $manfile.gz"); }'

然后选择新安装:

VirtualBox:/usr/lib/jvm$ sudo update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-u45-amd64 1070 /usr/lib/jvm/java-1.8.0-u45-amd64

VirtualBox:/usr/lib/jvm$ sudo update-java-alternatives -s java-1.8.0-u45-amd64

1

我在脚本中输入了@ air-dex和@Anwar的内容。我知道@ test30也写了一个脚本,但是对我(脚本新手)来说太复杂了。我只是想把它发布在这里,以防有人发现它有用。

#!/bin/bash

# Script to update Java defaults. Run as root.

# Adjust according to Java version that you are installing
JDK_DIR_NAME="/opt/java/jdk1.8.0_102"

# All Java tools according to https://docs.oracle.com/javase/8/docs/technotes/tools/unix/intro.html
TOOLS="appletviewer extcheck jar java javac javadoc javah javap jdb jdeps keytool jarsigner policytool kinit klist ktab native2ascii rmic rmiregistry rmid serialver tnameserv idlj orbd servertool javapackager pack200 unpack200 javaws jcmd jconsole jmc jvisualvm schemagen wsgen wsimport xjc jps jstat jstatd jinfo jhat jmap jsadebugd jstack jrunscript"

for TOOL in $TOOLS; do
    CMD1="update-alternatives --install /usr/bin/$TOOL $TOOL $JDK_DIR_NAME/bin/$TOOL 1"
    # echo $CMD1
    $CMD1
    CMD2="update-alternatives --config $TOOL"
    # echo $CMD2
    $CMD2
done

0

要设置完整的jdk替代方案,可以使用以下命令:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_45/bin/java 2 --slave /usr/bin/appletviewer appletviewer  /usr/lib/jvm/jdk1.7.0_45/bin/appletviewer --slave /usr/bin/java-rmi.cgi java-rmi.cgi  /usr/lib/jvm/jdk1.7.0_45/bin/java-rmi.cgi --slave /usr/bin/jmap jmap  /usr/lib/jvm/jdk1.7.0_45/bin/jmap --slave /usr/bin/jstatd jstatd  /usr/lib/jvm/jdk1.7.0_45/bin/jstatd --slave /usr/bin/policytool policytool  /usr/lib/jvm/jdk1.7.0_45/bin/policytool --slave /usr/bin/servertool servertool  /usr/lib/jvm/jdk1.7.0_45/bin/servertool --slave /usr/bin/apt apt  /usr/lib/jvm/jdk1.7.0_45/bin/apt --slave /usr/bin/javac javac  /usr/lib/jvm/jdk1.7.0_45/bin/javac --slave /usr/bin/jcmd jcmd  /usr/lib/jvm/jdk1.7.0_45/bin/jcmd --slave /usr/bin/jps jps  /usr/lib/jvm/jdk1.7.0_45/bin/jps --slave /usr/bin/jvisualvm jvisualvm  /usr/lib/jvm/jdk1.7.0_45/bin/jvisualvm --slave /usr/bin/rmic rmic  /usr/lib/jvm/jdk1.7.0_45/bin/rmic --slave /usr/bin/tnameserv tnameserv  /usr/lib/jvm/jdk1.7.0_45/bin/tnameserv --slave /usr/bin/extcheck extcheck  /usr/lib/jvm/jdk1.7.0_45/bin/extcheck --slave /usr/bin/javadoc javadoc  /usr/lib/jvm/jdk1.7.0_45/bin/javadoc --slave /usr/bin/jconsole jconsole  /usr/lib/jvm/jdk1.7.0_45/bin/jconsole --slave /usr/bin/jrunscript jrunscript  /usr/lib/jvm/jdk1.7.0_45/bin/jrunscript --slave /usr/bin/keytool keytool  /usr/lib/jvm/jdk1.7.0_45/bin/keytool --slave /usr/bin/rmid rmid  /usr/lib/jvm/jdk1.7.0_45/bin/rmid --slave /usr/bin/unpack200 unpack200  /usr/lib/jvm/jdk1.7.0_45/bin/unpack200 --slave /usr/bin/idlj idlj  /usr/lib/jvm/jdk1.7.0_45/bin/idlj --slave /usr/bin/javafxpackager javafxpackager  /usr/lib/jvm/jdk1.7.0_45/bin/javafxpackager --slave /usr/bin/jdb jdb  /usr/lib/jvm/jdk1.7.0_45/bin/jdb --slave /usr/bin/jsadebugd jsadebugd  /usr/lib/jvm/jdk1.7.0_45/bin/jsadebugd --slave /usr/bin/native2ascii native2ascii  /usr/lib/jvm/jdk1.7.0_45/bin/native2ascii --slave /usr/bin/rmiregistry rmiregistry  /usr/lib/jvm/jdk1.7.0_45/bin/rmiregistry --slave /usr/bin/wsgen wsgen  /usr/lib/jvm/jdk1.7.0_45/bin/wsgen --slave /usr/bin/jar jar  /usr/lib/jvm/jdk1.7.0_45/bin/jar --slave /usr/bin/javah javah  /usr/lib/jvm/jdk1.7.0_45/bin/javah --slave /usr/bin/jhat jhat  /usr/lib/jvm/jdk1.7.0_45/bin/jhat --slave /usr/bin/jstack jstack  /usr/lib/jvm/jdk1.7.0_45/bin/jstack --slave /usr/bin/orbd orbd  /usr/lib/jvm/jdk1.7.0_45/bin/orbd --slave /usr/bin/schemagen schemagen  /usr/lib/jvm/jdk1.7.0_45/bin/schemagen --slave /usr/bin/wsimport wsimport  /usr/lib/jvm/jdk1.7.0_45/bin/wsimport --slave /usr/bin/jarsigner jarsigner  /usr/lib/jvm/jdk1.7.0_45/bin/jarsigner --slave /usr/bin/javap javap  /usr/lib/jvm/jdk1.7.0_45/bin/javap --slave /usr/bin/jinfo jinfo  /usr/lib/jvm/jdk1.7.0_45/bin/jinfo --slave /usr/bin/jstat jstat  /usr/lib/jvm/jdk1.7.0_45/bin/jstat --slave /usr/bin/pack200 pack200  /usr/lib/jvm/jdk1.7.0_45/bin/pack200 --slave /usr/bin/serialver serialver  /usr/lib/jvm/jdk1.7.0_45/bin/serialver --slave /usr/bin/xjc xjc  /usr/lib/jvm/jdk1.7.0_45/bin/xjc

在将sun jdk1.7的tar.gz文件提取到/ usr / lib / jvm /之后,使用了此文件

您可能想用具有的版本的目录名替换jdk1.7.0,并将数字2替换为您希望的替代版本号。


0

我正在使用类似以下的内容来下载并安装最新的Java 8:

JDK_ALT_LINK_JAVA=/usr/bin/java
URL_TO_DOWNLOAD='http://www.java.net/download/jdk8u60/archive/b17/binaries/jdk-8u60-ea-bin-b17-linux-x64-26_may_2015.tar.gz'

JDK8_ARCHIVE=$(basename $URL_TO_DOWNLOAD)

wget "${URL_TO_DOWNLOAD}"
tar zxf "${JDK8_ARCHIVE}"
mv jdk1.8.0_60/ /usr/lib/jvm/
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_60/bin/java 1008000060

0

#我写了简单的脚本

(
lookforJdks=~
echo $lookforJdks
jdks=`test -e ./javac || find $lookforJdks -type d -iname '*jdk1.*' 2> /dev/null`
#set -e
echo 'which jdk do you want to choose? looking for jdks. This might take a while'
echo $"$jdks" | awk '{printf("%5d : %s\n", NR,$0)}'
read choose
test -e ./javac || cd `echo $"$jdks" | cut -d' ' -f $choose`/bin
for e in 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 jvisualvm jmc; do sudo update-alternatives \
 --install /usr/bin/$e $e $(readlink -f ./$e) 100; done
)

0

制作了另一个稍有不同的脚本,以将所有与Java相关的内容默认为手动安装的JDK:

#!/bin/bash

# Run update-alternatives for a manual installed JDK

JAVA_HOME=/opt/java/jdk1.8.0_152
java_bins=(appletviewer extcheck idlj jarsigner java javac javadoc javafxpackager javah javap javapackager java-rmi.cgi javaws jcmd jconsole jcontrol jdb jdeps jhat jinfo jjs jmap jps jrunscript jsadebugd jstat jstatd jvisualvm keytool native2ascii orbd pack200 policytool rmic rmid schemagen servertool tnameserv wsgen wsimport xjc jar jmc jmc.ini jstack rmiregistry serialver unpack200)

for java_bin in ${java_bins[@]}; do
    echo "Setting $java_bin..."
    update-alternatives --install /usr/bin/$java_bin $java_bin $JAVA_HOME/bin/$java_bin 1
    update-alternatives --set $java_bin $JAVA_HOME/bin/$java_bin
done

echo "Done."

这将安装新的替代方案并将这些新的替代方案设置为默认值。


0

我已经清除了有关JAVA 9的所有内容,并手动安装了新的Java 10。

注意仅当您有多个Java版本时才需要update-alternatives

注意:在重新安装新Java之前,您可以清除有关Java的所有内容。

 sudo apt purge java*

对于Java 10全新安装

  1. 从此处下载JDK 10(可以下载JRE和服务器JRE):http : //www.oracle.com/technetwork/java/javase/downloads/index.html
  2. 提取并放在/opt/java目录中的某个位置。您将JDK放在opt/java目录中将使其对所有用户可用

    注意:我下载了JDK,JRE和服务器JRE,然后提取JDK并将其重命名提取到文件夹jdk10,提取了JRE并将其重命名提取了文件夹到jre10,然后提取了服务器JRE并将其重命名提取了文件夹到jre10server,如下面的快照所示)。

  3. 如下更新您的/ etc / environment文件 在此处输入图片说明

现在,您需要/etc/environment使用如下source命令将脚本文件重新加载到系统中。

source /etc/environment

在此处输入图片说明

如果您想了解更多有关更新替代方法的信息,请访问以下链接:如何将Java默认设置为手动安装的JRE / JDK?


-1

一些与Java相关的链接处于主级别,不能在update-alternatives --install中用作从属。使用以下命令来安装oracle jdk和更新替代方案。希望能帮助到你。

update-alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_45/bin/java 10000 --slave /usr/bin/appletviewer appletviewer  /usr/java/jdk1.7.0_45/bin/appletviewer --slave /usr/bin/java-rmi.cgi java-rmi.cgi  /usr/java/jdk1.7.0_45/bin/java-rmi.cgi --slave /usr/bin/jmap jmap  /usr/java/jdk1.7.0_45/bin/jmap --slave /usr/bin/jstatd jstatd  /usr/java/jdk1.7.0_45/bin/jstatd --slave /usr/bin/apt apt  /usr/java/jdk1.7.0_45/bin/apt --slave /usr/bin/javac javac  /usr/java/jdk1.7.0_45/bin/javac --slave /usr/bin/jcmd jcmd  /usr/java/jdk1.7.0_45/bin/jcmd --slave /usr/bin/jps jps  /usr/java/jdk1.7.0_45/bin/jps --slave /usr/bin/jvisualvm jvisualvm  /usr/java/jdk1.7.0_45/bin/jvisualvm --slave /usr/bin/rmic rmic  /usr/java/jdk1.7.0_45/bin/rmic --slave /usr/bin/extcheck extcheck  /usr/java/jdk1.7.0_45/bin/extcheck --slave /usr/bin/javadoc javadoc  /usr/java/jdk1.7.0_45/bin/javadoc --slave /usr/bin/jconsole jconsole  /usr/java/jdk1.7.0_45/bin/jconsole --slave /usr/bin/jrunscript jrunscript  /usr/java/jdk1.7.0_45/bin/jrunscript --slave /usr/bin/idlj idlj  /usr/java/jdk1.7.0_45/bin/idlj --slave /usr/bin/javafxpackager javafxpackager  /usr/java/jdk1.7.0_45/bin/javafxpackager --slave /usr/bin/jdb jdb  /usr/java/jdk1.7.0_45/bin/jdb --slave /usr/bin/jsadebugd jsadebugd  /usr/java/jdk1.7.0_45/bin/jsadebugd --slave /usr/bin/native2ascii native2ascii  /usr/java/jdk1.7.0_45/bin/native2ascii --slave /usr/bin/wsgen wsgen  /usr/java/jdk1.7.0_45/bin/wsgen --slave /usr/bin/jar jar  /usr/java/jdk1.7.0_45/bin/jar --slave /usr/bin/javah javah  /usr/java/jdk1.7.0_45/bin/javah --slave /usr/bin/jhat jhat  /usr/java/jdk1.7.0_45/bin/jhat --slave /usr/bin/jstack jstack  /usr/java/jdk1.7.0_45/bin/jstack --slave /usr/bin/schemagen schemagen  /usr/java/jdk1.7.0_45/bin/schemagen --slave /usr/bin/wsimport wsimport  /usr/java/jdk1.7.0_45/bin/wsimport --slave /usr/bin/jarsigner jarsigner  /usr/java/jdk1.7.0_45/bin/jarsigner --slave /usr/bin/javap javap  /usr/java/jdk1.7.0_45/bin/javap --slave /usr/bin/jinfo jinfo  /usr/java/jdk1.7.0_45/bin/jinfo --slave /usr/bin/jstat jstat  /usr/java/jdk1.7.0_45/bin/jstat --slave /usr/bin/serialver serialver  /usr/java/jdk1.7.0_45/bin/serialver --slave /usr/bin/xjc xjc  /usr/java/jdk1.7.0_45/bin/xjc
update-alternatives --install /usr/bin/policytool policytool /usr/java/jdk1.7.0_45/bin/policytool 10000
update-alternatives --install /usr/bin/servertool servertool  /usr/java/jdk1.7.0_45/bin/servertool 10000
update-alternatives --install /usr/bin/tnameserv tnameserv  /usr/java/jdk1.7.0_45/bin/tnameserv 10000
update-alternatives --install /usr/bin/keytool keytool  /usr/java/jdk1.7.0_45/bin/keytool 10000
update-alternatives --install /usr/bin/rmid rmid  /usr/java/jdk1.7.0_45/bin/rmid 10000
update-alternatives --install /usr/bin/unpack200 unpack200  /usr/java/jdk1.7.0_45/bin/unpack200  10000
update-alternatives --install /usr/bin/rmiregistry rmiregistry  /usr/java/jdk1.7.0_45/bin/rmiregistry 10000
update-alternatives --install /usr/bin/orbd orbd  /usr/java/jdk1.7.0_45/bin/orbd 10000
update-alternatives --install /usr/bin/pack200 pack200  /usr/java/jdk1.7.0_45/bin/pack200 10000

PS:应以root / sudo身份执行。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.