Apt-get won't overwrite the existing java versions.

To switch between installed java versions, use the update-java-alternatives command.

List all java versions:

update-java-alternatives --list

Set java version as default (needs root permissions):

sudo update-java-alternatives --set /path/to/java/version

...where /path/to/java/version is one of those listed by the previous command (e.g. /usr/lib/jvm/java-7-openjdk-amd64).


Additional information:

update-java-alternatives is a convenience tool that uses Debian's alternatives system (update-alternatives) to set a bunch of links to the specified java version (e.g. java, javac, ...).

Answer from danzel on askubuntu.com
Top answer
1 of 16
2562

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

or you can specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now when you run java -version you will see:

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Add the export JAVA_HOME… line to your shell’s init file.

For Bash (as stated by antonyh):

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

For Fish (as stated by ormurin)

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

Updating the .zshrc file should work:

nano ~/.zshrc

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)

Press CTRL+X to exit the editor Press Y to save your changes

source ~/.zshrc
echo $JAVA_HOME
java -version
2 of 16
608

This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).

As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

Solution without 3rd party tools:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

System launcher will use the JDK with highest version among those that have an Info.plist file.

When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).


Details of investigation in this gist.


(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.

🌐
HappyCoders.eu
happycoders.eu › java › how-to-switch-multiple-java-versions-windows
How to Change Java Versions in Windows (up to Java 25)
June 11, 2025 - As the default version, I recommend the current release version, Java 24. Accordingly, you should make the following settings: The top list ("User variables") should not contain any Java-related entries. The lower list ("System variables") should contain an entry "JAVA_HOME = C:\Program Files\Java\jdk-24". If this entry does not exist, you can add it with "New…". If it exists but points to another directory, you can change it with "Edit…".
🌐
IBM
ibm.com › support › pages › changing-default-version-jdk-javaversion
Changing Default Version for JDK (java.version)
May 2, 2025 - Setting the job (*JOB) and default (*SYS) JDK version for the IBM Technology for Java VM on IBM i OS
Find elsewhere
🌐
Oracle
java.com › en › download › help › java_update.html
What is Java Update and how do I change the update schedule?
When you have auto update enabled, your system periodically checks for new versions of Java. When a new version is found we ask your permission to upgrade your Java installation. You can schedule how often to check for updates or check manually at any time. Change the update schedule through the Advanced settings of the Update tab.
🌐
Besu-eth
docs.besu-eth.org › how to › install and update java
Install and update Java | Besu documentation
June 3, 2026 - java -version · You might need to update your environment to make Java visible to Besu. Edit the .bashrc file in your home directory (or create it if needed) and add the following lines to the end of the file: .bashrc · export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java)))) export PATH=$PATH:$JAVA_HOME/bin · Save your changes and source the file: source ~/.bashrc ·
🌐
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 - # ~/.bashrc # Set JAVA_HOME to the current default Java export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::") export PATH=$JAVA_HOME/bin:$PATH · This dynamic approach picks up update-alternatives changes when you open a new shell or source the file again. For a fixed version: export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH ·
🌐
Minecraft Hopper
minecrafthopper.net › help › guides › changing-java-version
Changing Java Version | MinecraftHopper
January 15, 2026 - Press Shft-Cmd-G to open the Go menu, then type (or paste) /Library/Java/JavaVirtualMachines/ and press enter. Double-click the folder that matches the version of Java you want (most likely temurin-17.jdk), then go to Contents -> Home -> bin and find a file named java and click on it.
🌐
Oracle
oracle.com › java › technologies › downloads
Download the Latest Java LTS Free
Subsequent JDK 21 updates will be licensed under the Java SE OTN License (OTN) and production use beyond the limited free grants of the OTN license will require a fee. ... The URLs listed above will remain the same for update releases to allow their use in scripts. Learn more about automating these downloads · Gain expert support, rigorously tested Oracle JDK updates including all supported versions and long-term support for production, fleet automation with Java Management Service, BPRs for urgent customer reported issues, and more with an Oracle Java SE subscription.
Top answer
1 of 8
151

Re your first question:

possibly you may be confusing that the webupd8 script is 0.5b. That is the version of the script - it doesnt refer to the java version.

Further to the setting of the javac version.

I suspect you need to explicitly give the path of the javac compiler

i.e.

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-6.31-oracle/bin/javac" 1

followed by:

sudo update-alternatives --config javac

With regards to setting up the java chrome plugin.

The master question:

How do I install Oracle JDK 6?

includes this information - since your folder structure is slightly different your link command should be:

ln -s /usr/lib/jvm/java-6.31-oracle/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins/
2 of 8
104

See this; run

sudo  update-java-alternatives --list

to list off all the Java installations on a machine by name and directory, and then run

sudo  update-java-alternatives --set [JDK/JRE name e.g. java-8-oracle]

to choose which JRE/JDK to use.

If you want to use different JDKs/JREs for each Java task, you can run update-alternatives to configure one java executable at a time; you can run

sudo  update-alternatives --config java[Tab]

to see the Java commands that can be configured (java, javac, javah, javaws, etc). And then

sudo  update-alternatives --config [javac|java|javadoc|etc.]

will associate that Java task/command to a particular JDK/JRE.

You may also need to set JAVA_HOME for some applications: from this answer you can use

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

for JREs, or

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

for JDKs.

🌐
Medium
medium.com › towardsdev › how-to-change-java-jdk-versions-on-macos-erselan-khan-fb04abf57a6a
How to change Java (JDK) Versions on MacOS | Erselan Khan | by Erselan Khan | Towards Dev
May 27, 2024 - Set the JAVA_HOME variable to the desired version by using the path from the previous command: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home ...
🌐
Medium
pradiptasarma.medium.com › manage-multiple-jdk-versions-on-your-computer-d28bc7272775
Manage Multiple JDK Versions on Your Computer | by Pradipta Sarma | Medium
November 12, 2020 - Although, you can pick up a JAR ... for your actual projects. To change a version of Java, you need to change the JAVA_HOME environment variable....
🌐
MTU Service Desk
servicedesk.mtu.edu › TDClient › 1801 › Portal › KB › ArticleDet
Article - Changing Java versions (Linux)
These scripts are located at the following location: `/opt/java-selection/` In order to activate a Java version, run the script corresponding to the version you would like to work with, then reload or resource your terminal: For example the ...
🌐
Oracle
java.com › en › download › help › update_runtime_settings.html
Update Java runtime settings on Windows
Verify that the latest Java Runtime version is enabled by checking the Enabled box. ... Click OK in Java Control Panel window to confirm changes and close the window.
🌐
Medium
medium.com › @petehouston › switch-java-versions-on-any-systems-you-like-f0c996b4f57a
Switch Java versions on any systems you like | by Pete Houston | Medium
October 8, 2016 - If you’re working on many Java projects, you will be in situation of switching Java version continuously. I face this too. This is how to do it. ... and then, create one or several batch files to switch version, which will enable the ability to change the JAVA_HOME variable just by one click.