Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:

  1. Install the alternative for javac

    $ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1
    
  2. Check / update the alternatives config:

    $ sudo update-alternatives --config javac
    

If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.

To check everything is setup correctly then:

$ which javac
/usr/bin/javac

$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  4 17:10 /usr/bin/javac -> /etc/alternatives/javac

$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep  4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac

And finally

$ javac -version
javac 1.8.0_144

Repeat for java, keytool, jar, etc as needed.

Answer from Richard Grimshaw on Stack Overflow
Top answer
1 of 12
84

Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:

  1. Install the alternative for javac

    $ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1
    
  2. Check / update the alternatives config:

    $ sudo update-alternatives --config javac
    

If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.

To check everything is setup correctly then:

$ which javac
/usr/bin/javac

$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  4 17:10 /usr/bin/javac -> /etc/alternatives/javac

$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep  4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac

And finally

$ javac -version
javac 1.8.0_144

Repeat for java, keytool, jar, etc as needed.

2 of 12
76

You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:

When using the --config option, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be in auto mode. You will need to use the --auto option in order to return to the automatic state.

And I believe auto mode is set when you install the first/only JRE/JDK.

🌐
DEV Community
dev.to › thegroo › install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93
Install and manage multiple Java versions on Linux using alternatives - DEV Community
February 10, 2022 - sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/azul-open-jdk-17/bin/jshell 2 · Now that you have the new JDK installed and configured you can easily switch between versions using: ... Pick the number of the version you ...
Discussions

alternatives java -OpenJDK RHEL - Unix & Linux Stack Exchange
Which modified /etc/alternatives/java but due to the fact that $JAVA_HOME=/etc/alternatives/jre_1.8.0_openjdk I need to modify/update /etc/alternatives/jre_1.8.0_openjdk . I am not sure what to put as the in alternatives --install in order to update ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
July 13, 2020
Any Linux alternatives to Filebot?

It works on most platforms, including Linux. https://github.com/tinyMediaManager/tinyMediaManager

More on reddit.com
🌐 r/DataHoarder
43
16
December 11, 2018
Manage Multiple JDKs on Mac OS, Linux and Windows WSL2

Use these tools if you find them convenient, but know that a lot of effort has gone into making the JDK not require any setup, nor does it have any notion of "environment". You can set JAVA_HOME, you can leave JAVA_HOME pointed at a symbolic link and change that -- this is what SDKMAN does anyway -- or you can do nothing and just run java from its JDK directory -- it will do the right thing on its own. That's how the JDK was designed to work. Such tools add an unnecessary layer of internal persisted state, and unlike the language runtimes that inspired them, they are something that the JDK was specifically designed to avoid.

More on reddit.com
🌐 r/java
31
14
April 25, 2020
asdf - An extendable version manager
Frankly, I just gave up with these virtualenv tools and now run a container. I can see this working with java, because all you really need is JAVA_HOME="dummy dir path" PATH="$PATH:$JAVA_HOME/bin" ln -s "actual java installation" "dummy dir path" while in windows it would be adding %JAVA_HOME%/bin and changing the JAVA_HOME on demand. It works, because it doesn't do that many changes to underlying system. But for tools like ruby or python that enjoy shitting up bin directories, lib directories and what ever while having a tool that globally installs dependencies I just can't see this working. Note: the above only happens if you freely use gem install or pip install instead of using a build tool. But even then, in poor configuration it ends up leaking into other projects. More on reddit.com
🌐 r/programming
18
5
May 22, 2021
Top answer
1 of 6
7

When you install JDK on Linux, what gets installed depends on the type of package, version and distribution. You can refer to the following links for information about the installation location on linux:

JDK Installation for Linux Platforms - Version 8

JDK Installation for Linux Platforms - Version 7

Once you install JDK, the bin folder containing tools might not get added to the environment variable PATH. Commands typed on the terminal needs to be from the locations specified in the PATH variable. In cases when JDK\bin does not get added, the user would need to configure it manually as mentioned in Installing the JDK Software

alternatives command is being used to create a symbolic link. Here, it is being directed to use the command to add the tools like javac, javaw to /usr/bin which exists in the PATH variable by default.

If( you could execute java -version outside of JDK/bin && not by specifying the complete path && if the version and bundle prints to be as that of package you installed ){ you need not run the alternatives command.}

2 of 6
4

java, javaws, javac, and jar are all different executables used by the JDK. When you run alternatives --config java you are only configuring which version of the java executable you wish to run. However, the JDK includes, for example, the javac compiler. You need to configure which version of the compiler you wish to use as well.

The alternatives command is, in a nutshell, used to maintain a lookup for symbolic (or sym) links. Before you can choose which version of java you want to run with the --configure option, you must first register the actual path to the executable with the --install option. alternatives --install is not installing any software. It is merely registering some paths and aliases with the alternatives framework. (Note: alternatives is not using the alias command. I mean "aliases" in the traditional, literal sense.)

You should also understand what the rpm command does. Really, it is only dropping down a set of binaries into a particular directory. This directory may be long and tedious to explicitly specify: /some/path/to/lib/jvm/java-1.x.x-etc-etc-x86/jre/bin/java. You don't want to specify this every time you want to run java. Instead, we set up some sym links.

You might also want to read up on how the PATH works in linux.

It may become clearer if you try tracing through the setup for your machine. Here is what I ran:

> which java
/usr/bin/java
> ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Aug 14  2014 /usr/bin/java -> /etc/alternatives/java
> ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 73 Aug 14  2014 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65-2.5.1.2.fc19.x86_64/jre/bin/java
🌐
LinuxVox
linuxvox.com › blog › when-and-why-run-alternatives-install-java-jar-javac-javaws-on-installing-jdk-in-linux
When and Why to Run `alternatives --install` for java, jar, javac, javaws When Installing JDK on Linux (CentOS/RHEL) — linuxvox.com
For Java, this tool is indispensable when installing or upgrading JDKs, as it ensures the system uses the correct java, javac, jar, and javaws binaries across all applications. In this blog, we’ll demystify the alternatives system, explain when and why you need to run alternatives --install for Java commands, and provide a step-by-step guide to configure JDKs on CentOS/RHEL.
Top answer
1 of 4
213
sudo update-alternatives --config java

Configures the default for the program "java". That's the Java VM.

sudo update-alternatives --config javac

Configures the default Java compiler.

You can also see that, because the first command lists a lot of "JRE" (Java Runtime Environment) folders and the Program is just called "java".

If I check which version is being used by issuing the command java -version or javac -version, I can see, that each command changes the program being used.

However, using update-java-alternatives with a JDK Version changes both programs for me. Using the first commands, you can use a Java VM and Java Compiler from different JDKs.

update-java-alternatives requires presence of a file with extension .jinfo in directory /usr/lib/jvm. The openjdk package is shipped with a .jinfo file, the jdk of Oracle (formerly Sun) is not. As alternative, you configure alternatives without update-java-alternatives:

For example, to add java from jvm-directory /usr/lib/jvm/jdk-12.0.1 (default directory of Debian package of Oracle) with priority 2082, use the following command:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2082

As for switching for different development environments:

Are you talking about starting the IDE itself with different Java versions or using different versions in the IDE for compilation and running your app?

  • For 1.: You can specify which JVM to use in the eclipse.ini, as described here. I don't know how to do that for the Arduino IDE.

  • For 2.: In Eclipse you can select the JRE/JDK to be used in Window -> Preferences -> Java -> Installed JREs. And under Java -> Compiler you could choose an older Java compliance if you wish.

EDIT: This DigitalOcean page also has a very nice explanation of everything related to Java on Ubuntu.

2 of 4
36

update-java-alternatives is a program to update alternatives for jre/jdk installations.

update-alternatives is a symbolic link management system for linux (I'm sure there is little news here).

You can, and really should, use both update-java-alternatives and update-alternatives together.

Firstly, be sure to have the all the alternatives configured correctly. java and javac are but a few. There is javadoc, rmic, serialver and others, substituting the above variables for: native2ascii and /opt/jdk1.8.0_40/bin/native2ascii should report if the alternative is installed and/or selected.

When all the alternatives are configured you can then create links in /usr/lib/jvm to your manual instalation.

In order to configure update-java-alternatives you must use a hidden file with the same name as your directory but prefixed by a . (dot).

Hope this helps.

Bibliography

man -S 8 update-java-alternatives

http://tech.lanesnotes.com/2008/03/using-alternatives-in-linux-to-use.html

https://stackoverflow.com/questions/6477415/how-to-set-oracles-java-as-the-default-java-in-ubuntu

🌐
GitHub
gist.github.com › bench › 3935f5e72c7dca83a4e31210b2c305e9
java and javac update-alternatives · GitHub
$ sudo update-alternatives --install /usr/bin/javac javac /home/bchenebault/Apps/jdk1.5.0_22/bin/javac 1 $ sudo update-alternatives --install /usr/bin/javac javac /home/bchenebault/Apps/jdk1.6.0_45/bin/javac 2 $ sudo update-alternatives --install /usr/bin/javac javac /home/bchenebault/Apps/jjdk1.7.0_79/bin/javac 3 $ sudo update-alternatives --install /usr/bin/javac javac /home/bchenebault/Apps/jdk1.8.0_101/bin/javac 4
🌐
CentOS
wiki.centos.org › HowTos › JavaRuntimeEnvironment
How to install the Java Runtime Environment from Java.com
Selection Command ----------------------------------------------- 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java 2 /usr/java/jre1.6.0_13/bin/java 3 /usr/java/jre1.6.0_18/bin/java *+ 4 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java Enter to keep the current selection[+], or type selection number: Note number of versions, N, and use N+1 (if there is no output from the previous command, just imagine that N=0) - For example, if 4 versions were installed then: # alternatives --install /usr/bin/java java /usr/java/latest/bin/java 5 # alternatives --config java There are 5 programs which provide 'java'.
Find elsewhere
🌐
Medium
medium.com › @ayeshajayasankha › how-to-install-and-switch-between-alternative-java-versions-66b3671fc382
How To Install And Switch Between Alternative Java Versions | by Ayesha Jayasankha | Medium
July 3, 2019 - export JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0export PATH=$PATH:$HOME/bin:$JAVA_HOME/bin ... sudo update-alternatives — install “/usr/bin/java” “java” “<Directory where JAVA has been extracted>/bin/java” 1sudo ...
🌐
Red Hat
access.redhat.com › solutions › 1161843
How to manually install a new java path to the alternatives java group? - Red Hat Customer Portal
How to manually install a new Java path to the alternatives java group? How to add Oracle's Java to the alternatives utility?
🌐
Django CAS
djangocas.dev › blog › linux › switch-java-version-with-update-alternatives
Switch Java Version with update-alternatives - django-cas-ng
July 7, 2024 - In Debian/Unbutu, how to install different version of java and use update-alternatives/update-java-alternatives to switch different version of java
🌐
Igalia
blogs.igalia.com › dpino › 2011 › 10 › 13 › configuring-different-jdks-with-alternatives
Configuring different JDKs with alternatives - Unweaving the Web
$ alternatives --config java + 1 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java * 2 /etc/alternatives/java_sdk/bin/java 3 /usr/lib/jvm/jre-1.5.0-gcj/bin/java 4 /usr/java/latest/bin/java 5 /usr/java/jdk1.7.0/bin/java · It seems I got several If you just installed Fedora and run the command above, probably you’ll see nothing, as you haven’t added an alternative yet.
🌐
GitHub
gist.github.com › jeffcogswell › 8c47fb21355faf346a7336998ce76e07
Update Alternatives for Java · GitHub
Some good info here: ... ~/Downloads/jdk-8u91-linux-x64.tar.gz sudo chown -R root:root jdk1.8.0_91 sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_91/bin/java 1 sudo update-alternatives --install ...
🌐
Super User
superuser.com › questions › 1576013 › how-to-use-sudo-update-alternative-for-java-installation-on-ubuntu-18-04
How to use sudo update-alternative for java installation on Ubuntu 18.04? - Super User
August 7, 2020 - But it works perfectly for javac. ... Ater installing the new java, you would need to make it the java pointed to by /usr/bin/java, by typing
🌐
Red Hat
access.redhat.com › solutions › 6232511
How to set default Java version with alternatives tool in RHEL on a per user basis - Red Hat Customer Portal
5 days ago - When different Java versions are installed on a RHEL system, using the alternatives tool as a regular user fails to set the default version. # alternatives --config java There is 2 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 ...
🌐
LinuxVox
linuxvox.com › blog › why-is-alternatives-command-used-when-installing-java-on-a-linux-machine
Why Use the Alternatives Command When Installing Java on Linux? Explaining the 20000 Value — linuxvox.com
For example, /usr/bin/java is typically a symlink to the actual Java executable (e.g., /usr/lib/jvm/java-17-openjdk/bin/java). When you install multiple Java versions, you end up with multiple such executables, and manually updating symlinks every time you switch versions is error-prone. ... Maintaining a central directory (/etc/alternatives) where "master" symlinks live.
🌐
Medium
medium.com › @adilmustafa006_32164 › install-and-manage-multiple-java-versions-on-linux-using-alternatives-585ac71c23d7
Install and manage multiple Java versions on Linux using alternatives | by Adil Mustafa | Medium
May 1, 2024 - sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/azul-open-jdk-17/bin/jshell 2 · Now that you have the new JDK installed and configured you can easily switch between versions using: ... Pick the number of the version you ...
Top answer
1 of 14
62

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.
2 of 14
39

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):

🌐
OneUptime
oneuptime.com › home › blog › how to install and switch java versions on ubuntu
How to Install and Switch Java Versions on Ubuntu
March 2, 2026 - # Check if Java is installed and which version java -version # Check the compiler version javac -version # Find where Java is installed which java readlink -f $(which java) # Show all installed Java versions update-alternatives --list java