Copy/usr/libexec/java_home -V
lists one line per Java environment installed (and known to the /usr/bin/java command).
You can still have other Java distributions which are not registered. They are typically downloaded as ZIP files without an installer, or using another package manager like homebrew.
Answer from Thorbjørn Ravn Andersen on Stack OverflowCopy/usr/libexec/java_home -V
lists one line per Java environment installed (and known to the /usr/bin/java command).
You can still have other Java distributions which are not registered. They are typically downloaded as ZIP files without an installer, or using another package manager like homebrew.
The accepted solution didn't work for me.
The results it returned didn't include all the Java versions installed. For example on my machine there are currently 5 installations, but the accepted solution only returns 3.
What eventually did work for me is:
Copymdfind -name 'java' | grep '/bin/java$'
It finds all java installations on the system regardless of how they were installed. This way I found the specific java installation which I was looking for.
Explanation of how it works:
mdfind is a native tool in MacOS to search for different files by given query, it's very fast (usually way faster than find)
The given command is looking for everything called java and then filter only the results that end with /bin/java which is the typical suffix of java installations
macos - Is there a way to find the complete list of Java versions installed on OS X? - Stack Overflow
macos - Mac OS X and multiple Java versions - Stack Overflow
Switching versions of Java on Mac OSX
Can I play Minecraft on a Mac? And Apple laptop ?
Videos
Mac OS X has a cool binary (/usr/libexec/java_home) that returns the path to a Java home directory from the current user's settings. You may want to issue:
Copy/usr/libexec/java_home -V
It prints the full JVM list with architectures like this:
CopyMatching Java Virtual Machines (2):
13, x86_64: "OpenJDK 13" /Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home
1.8.0_265, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home
If you want to print the JAVA_HOME path of a specific JDK version, you can do it like this:
Copy/usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
You can see the complete usage options of below:
CopyUsage: java_home [options...]
Returns the path to a Java home directory from the current user's settings.
Options:
[-v/--version <version>] Filter Java versions in the "JVMVersion" form 1.X(+ or *).
[-a/--arch <architecture>] Filter JVMs matching architecture (i386, x86_64, etc).
[-d/--datamodel <datamodel>] Filter JVMs capable of -d32 or -d64
[-t/--task <task>] Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
[-F/--failfast] Fail when filters return no JVMs, do not continue with default.
[ --exec <command> ...] Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
[-R/--request] Request installation of a Java Runtime if not installed.
[-X/--xml] Print full JVM list and additional data as XML plist.
[-V/--verbose] Print full JVM list with architectures.
[-h/--help] This usage information.
You can use the java_home binary for this task.
/usr/libexec includes internal binaries that are not intended to be executed directly by users or shell scripts. java_home is one such binary, calling it with option -V will print full JVM list with architectures.
Copy /usr/libexec/java_home -V
The cleanest way to manage multiple java versions on Mac is to use Homebrew.
And within Homebrew, use:
homebrew-caskto install the versions of javajenvto manage the installed versions of java
As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow.
- install homebrew
- install homebrew jenv
- install homebrew-cask
- install a specific java version using cask (see "homebrew-cask versions" paragraph below)
- add this version for jenv to manage it
- check the version is correctly managed by jenv
- repeat steps 4 to 6 for each version of java you need
homebrew-cask versions
Add the homebrew/cask-versions tap to homebrew using:
Copybrew tap homebrew/cask-versions
Then you can look at all the versions available by searching for Eclipse temurin, more details here
Copybrew search temurin
Then you can install the version(s) you like, though version 7 no longer shows up in homebrew via these casks:
Copybrew install --cask temurin8
brew install --cask temurin9
And add them to be managed by jenv as usual.
Copyjenv add <javaVersionPathHere>
I think this is the cleanest & simplest way to go about it.
Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing :
For different types of JDKs or installations, you will have different paths
You can check the paths of the versions installed using
/usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac?On Mac OS X Mavericks, I found as following:
Built-in JRE default:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/HomeJDKs downloaded from Apple:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/JDKs downloaded from Oracle:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
Resources
- Removing Java 8 JDK from Mac
- http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
- http://sourabhbajaj.com/mac-setup/index.html
- http://brew.sh
- https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
- http://sourabhbajaj.com/mac-setup/Homebrew/README.html
- "brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
- “brew versions” explained Homebrew install specific version of formula? and also https://github.com/Homebrew/homebrew-versions
- https://github.com/caskroom/homebrew-cask
- “cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versions and also https://github.com/caskroom/homebrew-cask/issues/9447
- http://www.jenv.be
- https://github.com/gcuisinier/jenv
Uninstall jdk8, install jdk7, then reinstall jdk8.
My approach to switching between them (in .profile) :
Copyexport JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
export JAVA_20_HOME=$(/usr/libexec/java_home -v20)
alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
alias java20='export JAVA_HOME=$JAVA_20_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
Then you can simply type java7 or java8 in a terminal to switch versions.
(edit: updated to add Dylans improvement for Java 9)
Hello!
I'm currently using Java 8 on my Mac and i'm finally getting around to the latest Java. I just installed JDK 19, and i'm wondering how i'm suppose to switch to the new jdk on my machine.
One doc says I can run this command: /usr/libexec/java_home -v 19 --exec javac -version
But my output just shows:
javac 1.8.0_291
Is there a standard way of switching java versions on Mac?