54

I have Ubuntu 12.04 amd64 installed on my machine, on the previous versions of Ubuntu it was deadly easy, now there is this command update-java-alternatives with a really bad man page.

I just have my JDK unpacked on a mounted partition like /media/mydisk/jdk, how i can force the use of that JDK instead of the one that comes in the Ubuntu repository?

What is the logic behind this update-java-alternatives ?

14 Answers 14

30

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.

As a graphical alternative to @AnwarShah's command-line method, consider using Gnome Alternatives (sudo apt-get install galternatives):

  • Start GAlternatives, select each java option, change it to manual, and Add your alternative (the example here is the IBM JRE installed in my home directory):

    enter image description here

    enter image description here

    enter image description here

41

You can do this with 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

You have to do this for other jdk components too. These components are appletviewer, extcheck, idlj, jar, jarsigner, java, javac, javadoc, javah, javap, javaws, jconsole, jdb, jexec, jhat, jinfo, jmap, jps, jrunscript, jsadebugd, jstack, jstat, jstatd, keytool, mozilla-javaplugin.so, native2ascii, orbd, pack200, policytool, rmic, rmid, rmiregistry, schemagen, serialver, servertool, tnameserv, unpack200, wsgen, wsimport, xjc.

For further information, you can see the man pages for update-alternatives : man update-alternatives.

The command update-java-alternatives sets alternatives for java components and it seems to use update-alternatives for this.

EDIT: The answer above targets Java 6.

  • For Java 7, you have to add jcmd to the Java 6 list.
  • For Java 8, you have to add jcmd, jjs and jdeps to the Java 6 list.
  • For Java 9, you have to add jcmd, jjs, jdeps and jshell to the Java 6 list.
  • It seems like a lot of the answers here mention different subsets of java files that needs to be installed. This answer by @air-dex for example mentions a whole bunch. Can anybody tell me which I need to install with a update-alternative call? Where can I find information on this? – thomas May 24 '15 at 8:39
  • @thomas Install all the alternatives I mention here through update-alternatives. My list contains all the Java commands of a JDK. – air-dex May 25 '15 at 9:35
  • 1
    also look at these JDK components: orbd, pack200, you might wanna change those too – Jaroslav Záruba Dec 27 '16 at 16:00
24

I actually use these commands to install jdk1.6.0_25. (Check the link below for updated complete update-alternatives script)

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

And then you can use these command to set as default. Most probably, these aren't needed.

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

You can get more help by using the command man update-alternatives or you can see the manual in your browser here

The purpose of the update-java-alternatives as described in the manual page is

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.

All the jdk tools are

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

Hope this will help you.


Update

I use this script to to extract a downloaded jdk, and use update-alternatives to fully set it. Modify the jdk file name and JAVA_DIR to suit your need. JAVA_DIR is the path to your extracted jdk folder.

#!/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 should be found beneath /media/mydisk/jdk/bin/javac – Jonathan Neufeld Mar 16 '16 at 18:42
3

You could also use this bash script :

#!/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
  • This would look cleaner as a loop :/ – cricket_007 Aug 7 '18 at 21:16
3

Too complex most of the answers for me.

Initially, Oracle decided to be able to have several versions of Java installed, based on setting some environment variables.

It was easy, but too complex for those who didn't know those variables, and somebody invented "update-java-alternatives".

"update-java-alternatives" has proven to be simple, when everything is configured for you, you simply have to execute this program, and choose the version you want.

The problem is that this solution is too complex to configure, if you have to configure yourself (you have to configure it for each and every command of java).

The best answer is go back to the basics.

set in your .bash_profile (for your user) or in /etc/profile (for every user) the following variables:

JAVA_HOME=<The home of your new java distribution>

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

In my case this was, even easier... I already had a file in /etc/profile.d with the following content (just updated it to the new directory structure):

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

And that's all!!!

1

It is not as complicated as the other answers make it out to be. You have to add the manual installed java to the alternatives list, then switch to is: (replace jdk1.7.0_79 with your version)

sudo /usr/sbin/alternatives --install /usr/java/jdk1.7.0_79/bin/java 20000
sudo /usr/sbin/alternatives --config java
  • 1
    You're missing two params to --install, it should be '/usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_79/bin/java 20000'. – gaoithe May 5 '15 at 12:05
1

update-java-alternatives is actually pretty nice. So, to get it to work with my manual installation: copy jdk1.8.0_45 to /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

Then I used vim for a search / replace:

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

Also decrement the priority by 1

:sigh: update-java-alternatives doesn't do the installation. But at least we have a file to work with

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"); }'

Then I selected my new installation:

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

I put what @air-dex and @Anwar said in a script. I know @test30 also wrote a script but it was too complicated for me (a scripting novice) to understand. I just thought I'd post it here in case somebody might find it useful.

#!/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

To set up a complete jdk alternative one can use the below command:

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

I used this after I extracted the tar.gz file for sun jdk1.7 to /usr/lib/jvm/

You would want to replace jdk1.7.0 with the directory name of the version you have and the number 2 to whatever number you want the alternative to be.

0

I'm using something like the following to download and install the latest 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

#I've written simple script

(
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

Made another slightly different script to default all java related stuff to a manually installed 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."

This installs the new alternatives and set these new alternatives as default.

0

I have purged everything about JAVA 9 and installed new fresh Java 10 manually.

Note: You need update-alternatives only if you have multiple java versions.

Note: You can purge everything about Java before fresh installing new Java.

 sudo apt purge java*

For Java 10 fresh installation

  1. Download JDK 10 from here (you can download JRE and server JRE): http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. Extract and put somewhere in /opt/java directory. You putting JDK in opt/java directory makes it will be usable for all users

    (Note: I downloaded JDK, JRE, and server JRE, and extracted JDK and renamed extracted the folder to jdk10, extracted JRE and renamed extracted the folder to jre10, and extracted server JRE and renamed extracted the folder to jre10server as shown in below snapshot).

  3. Update your /etc/environment file as below enter image description here

Now, you need to reload the /etc/environment script file into the system using source command as below.

source /etc/environment

enter image description here

If you want to know more about update-alternatives then here is the link: How do I make java default to a manually installed JRE/JDK?

-1

Some of java related links are at master level and cannot be used as slave in update-alternatives --install. Used the following commands to install oracle jdk and update alternatives. Hope it helps.

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: Should be executed as root/sudo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.