javac -version in a terminal will do
javac -version in a terminal will do
You can leverage the java_home helper binary on OS X for what you're looking for.
To list all versions of installed JDK:
Copy$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.0_51, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
1.7.0_79, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
To request the JAVA_HOME path of a specific JDK version, you can do:
Copy$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
You could take advantage of the above commands in your script like this:
CopyREQUESTED_JAVA_VERSION="1.7"
if POSSIBLE_JAVA_HOME="$(/usr/libexec/java_home -v $REQUESTED_JAVA_VERSION 2>/dev/null)"; then
# Do this if you want to export JAVA_HOME
export JAVA_HOME="$POSSIBLE_JAVA_HOME"
echo "Java SDK is installed"
else
echo "Did not find any installed JDK for version $REQUESTED_JAVA_VERSION"
fi
You might be able to do if-else and check for multiple different versions of java as well.
If you prefer XML output, java_home also has a -X option to output in XML.
Copy$ /usr/libexec/java_home --help
Usage: 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.
macos - See all the Java versions installed on Mac - Stack Overflow
Where is Java installed on MacOS?
macos - Determining the version of Java SDK on the Mac - Stack Overflow
Wrong Java version showing in Terminal on MacOS and Lubuntu.
Can I manage multiple Java versions on Mac?
How do I find the JDK version on Mac?
How do I update Java on macOS?
Videos
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.
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
On my system, I see that java is installed on /usr/bin AND /Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home/bin. When I type which java in terminal, it says /usr/bin. So, why is it installed in two directories? (I know I can change the directory by typing the second directory first in the $PATH file, but my question is why are there two binaries and if I should remove one of the two. Also, each file is unique and not an alias of the other). Thank you
Open a terminal and type: java -version, or javac -version.
If you have all the latest updates for Snow Leopard, you should be running JDK 1.6.0_20 at this moment (the same as Oracle's current JDK version).
In /System/Library/Frameworks/JavaVM.framework/Versions you'll see all the installed JDKs. There is a symbolic link named CurrentJDK pointing the active JDK.