51

Is there a way to quickly switch over all the Java related alternatives using update-alternatives?

For instance, if want to switch Java over to 7, I run sudo update-alternatives --config java and select the Java 7 OpenJdk. But if I run update-alternatives --get-selections | grep java I get the following,

appletviewer                   auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/appletviewer
extcheck                       auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/extcheck
idlj                           auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/idlj
itweb-settings                 auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/itweb-settings
jar                            auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jar
jarsigner                      auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jarsigner
java                           manual   /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
javac                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/javac
javadoc                        auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/javadoc
javah                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/javah
javap                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/javap
javaws                         auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/javaws
jconsole                       auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jconsole
jdb                            auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jdb
jexec                          auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/jexec
jhat                           auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jhat
jinfo                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jinfo
jmap                           auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jmap
jps                            auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jps
jrunscript                     auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jrunscript
jsadebugd                      auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jsadebugd
jstack                         auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jstack
jstat                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jstat
jstatd                         auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/jstatd
keytool                        auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/keytool
native2ascii                   auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/native2ascii
orbd                           auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/orbd
pack200                        auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/pack200
policytool                     auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/policytool
rmic                           auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/rmic
rmid                           auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/rmid
rmiregistry                    auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/rmiregistry
schemagen                      auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/schemagen
serialver                      auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/serialver
servertool                     auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/servertool
tnameserv                      auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/tnameserv
unpack200                      auto     /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/unpack200
wsgen                          auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/wsgen
wsimport                       auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/wsimport
xjc                            auto     /usr/lib/jvm/java-6-openjdk-amd64/bin/xjc

As you can see, my Java alternative was switched over to 7, but every other alternative based on OpenJDK 6 was not switched over. Sure I could switch each one manually or write a script to do so, but I assume there is a better way to accomplish this.

70

The trick is to use update-java-alternatives (from the java-common package).

So to go for OpenJDK 6 to 7, I used update-java-alternatives -l to get a list of Java alternatives and then used sudo update-java-alternatives -s java-1.7.0-openjdk-amd64 to the JDK.

NOTE: The command above threw the following errors,

update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so

This is because the openjdk plugin is not installed by default. To fix run

sudo apt-get install icedtea-7-plugin

and rerun update-java-alternatives.

  • 8
    You don't have to install the plugin (and you shouldn't if you don't need it, due to security considerations). update-java-alternatives changes all other java alternatives, even when the plugin setting throws an error. – daniel kullmann Apr 16 '13 at 12:17
  • What's with Oracles JVM? How can I use that? And the JDK specific executables? – keiki Mar 18 '14 at 19:42
  • 2
    Running update-java-alternatives --set java-1.8.0-openjdk-amd64 I still have default-java -> /usr/lib/jvm/java-7-oracle. Is this caused by the no alternatives for mozilla-javaplugin.so error? How can I change this as well? – lony Jan 16 '17 at 21:06
3

jbro asked the same question at http://ubuntuforums.org/showthread.php?t=1817528 and wrote a script for this issue,

#!/bin/bash

update-alternatives --get-selections | grep -i openjdk |
while read line
do
    alternative=$(echo $line | awk '{print $1}')
    path=$(echo $line | awk '{print $3}')
    newpath=$(echo $path | sed -e 's/java-6-openjdk/java-6-sun/')
    status=unchanged
    if [ -f $newpath ]
    then
    status=modified
    echo "-> update-alternatives --set $alternative $newpath"
    update-alternatives --set $alternative $newpath
    else
    echo "$alternative unchanged"
    fi
done

If there isn't a better answer this seems like a solid workaround, but I am assume there must be a better way for update-alternatives to handle this.

I edited the code a little, since it does not let you do the changes before installing them. I added two more lines as follows...

#!/bin/bash

update-alternatives --get-selections | grep -i openjdk |
while read line
do
    alternative=$(echo $line | awk '{print $1}')
    path=$(echo $line | awk '{print $3}')
    newpath=$(echo $path | sed -e 's/java-6-openjdk/java-6-sun/')
    status=unchanged
    if [ -f $newpath ]
    then
    status=modified
echo "-> update-alternatives --install /usr/bin/$alternative $alternative $newpath 1"
update-alternatives --install /usr/bin/$alternative $alternative $newpath 1
    echo "-> update-alternatives --set $alternative $newpath"
    update-alternatives --set $alternative $newpath
    else
    echo "$alternative unchanged"
    fi
done
1

For Oracle JVM, run this:

sudo update-java-alternatives -s java-7-oracle
0

< TL;DR > update-java-alternatives uses the .jinfo file to determine which alternatives to switch across but also expects them to have been configured separately.

Create a new .jinfo file and update-alterntive entries if your downloaded JRE/JDK does not switch over with update-java-alternatives

[Details]

To configure it manually:

  1. Decompress your new jdk alongside the existing jdk in /usr/lib/jvm/

  2. Generate a .jdkname.jinfo file by duplicating another .jinfo file in the folder (or grab one from another Ubuntu install: you are after the template as much as anything else).

    The filename .jdkname.jinfo must match the jdk foldername.

    Modify the content to set the header (names) and the file paths (program entries) to match your new install.

    Remove any lines referring to programs omitted in your version and add lines for new programs in /bin

  3. Generate alternatives for all the programs now in your .jinfo package with this template:

    sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/**jdkname**/bin/java 100
    

    (Several related answers have scripted this from the contents of /bin or you can perform regexp search/replace on your .jinfo file to generate the script commands.)

    Note the 100 is just the priority which is considered when setting the jdk version using the auto flag.

  4. Check the java alternatives and switch to use the JDK.

    update-java-alternatives -l  
    update-java-alternatives -s jdkname  
    

    Note that some JDKs do not include the JRE, so either add it or remove those jinfo entries.

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.