How do I make java default to a manually installed JRE/JDK?

192,136

Solution 1

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

Solution 2

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.

Solution 3

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/bin/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

Solution 4

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

Solution 5

Update: as @newur says in comments, just use sdkman

Original answer below


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.

Share:
192,136

Related videos on Youtube

flower
Author by

flower

Updated on September 18, 2022

Comments

  • flower
    flower over 1 year

    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 ?

  • gaoithe
    gaoithe about 9 years
    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'.
  • thomas
    thomas almost 9 years
    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?
  • air-dex
    air-dex almost 9 years
    @thomas Install all the alternatives I mention here through update-alternatives. My list contains all the Java commands of a JDK.
  • Jaroslav Záruba
    Jaroslav Záruba over 7 years
    also look at these JDK components: orbd, pack200, you might wanna change those too
  • OneCricketeer
    OneCricketeer almost 6 years
    This would look cleaner as a loop :/
  • newur
    newur over 4 years
    The list of java bins changes with every version a bit. Can be made more general with cd $JAVA_HOME/bin shopt -s nullglob java_bins=(*) This reads all file names in the JAVA_HOME/bin folder into the array.
  • iambr
    iambr almost 3 years
    2021+ done with this @newur that's excellent perfect thanks!
  • newur
    newur almost 3 years
    Glad it helped. Btw I would not hand roll any script for this in 2021. Just use sdkman from sdkman.io and enjoy the magic.
  • Juan
    Juan over 2 years
    Be careful reading all file names from $JAVA_HOME/bin, for example in java7 there is an executable called apt, and it could get in confict with apt command from ubuntu