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