Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
up vote 18 down vote accepted

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

share|improve this answer

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, javac, javadoc, javah, javap, javaws, jconsole, jdb, jexec, jhat, jinfo, jmap, jps, jrunscript, jsadebugd, jstack, jstat, keytool,, native2ascii, policytool, rmic, rmid, rmiregistry, schemagen, serialver, servertool, tnameserv, unpack200, wsgen, wsimport, xjc.

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

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.
share|improve this answer
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

I actually use these commands to install jdk1.6.0_25.

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.

share|improve this answer
javac should be found beneath /media/mydisk/jdk/bin/javac – Jonathan Neufeld Mar 16 at 18:42

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
share|improve this answer
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

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.

share|improve this answer

I'm using something like the following to download and install the latest Java 8:



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
share|improve this answer

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

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
share|improve this answer

#I've written simple script

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
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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