So, I have two different versions of Java in my system, 8 and 16. Depending on the project, I need to switch between the two. Right now, I need to use 16, but when I run java -version I see, I'm still using "1.8.0_202".
I have changed my JAVA_HOME variable to point to the location of Java 16. Do I need to uninstall Java 8 in order to use Java 16? I'd rather not do that.
How to set or change the default Java (JDK) version on macOS? - Stack Overflow
How do I change Java version in Java? - Ask a Question - TestMu AI Community
How do I switch between Java versions?
How do I change the default version of Java that Curse uses to launch Minecraft?
Videos
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, ...).
Use
sudo update-alternatives --config java
which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path...
------------------------------------------------------------
0 /usr/lib/jvm/java-9-oracle/bin/java...
* 1 /usr/lib/jvm/java-7-oracle/jre/bin/java...
2 /usr/lib/jvm/java-8-oracle/jre/bin/java...
3 /usr/lib/jvm/java-9-oracle/bin/java...
Press <enter> to keep...[*], or type selection number:
Use
export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"
to set $JAVA_HOME from current active version
Change your PATH variable so that it has the location of the jdk5/bin directory:
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
- Close the window.
- Reopen Command prompt window, and run
java -version
In the command shell:
set JAVA_HOME=C:\jdk1.6.0u24
set PATH=%JAVA_HOME%\bin;%PATH%
That will temporarily set up the environment in the command shell. Maven, Ant, etc. will pick up on your new version of Java without having to go to the Control Panel repeatedly.
Tools like Eclipse should be able to select which JDK to use in their own configuration tools for use within their environments.
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
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.plisttoInfo.plist.disabled. That JDK can still be used when$JAVA_HOMEpoints to it, or explicitly referenced in a script or configuration. It will simply be ignored by system'sjavacommand.
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.