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.
Wrong Java version showing in Terminal on MacOS and Lubuntu.
How to check Java on your macbook - Apple Community
Where is Java Installed on Mac OS X? - TechRepublic
Where is Java installed on MacOS?
Videos
Hi folks, I'm on MacOS 11.6 and have a Java JDK question - hopefully this is the right place for it.
I've got a Java assignment for which I'm required to use the Liberica 11 JDK. It's installed - Eclipse has it and can find it.
I used to also have an OpenJDK version installed, but I've removed that (or at least, I thought I have!)
In Terminal,
java -version
gives
openjdk version "11.0.10" 2021-01-19 LTS OpenJDK Runtime Environment (build 11.0.10+9-LTS) OpenJDK 64-Bit Server VM (build 11.0.10+9-LTS, mixed mode)
In Library/Java/Java Virtual Machines only the Liberica JDK is showing.
I've tried adding it to my path (not 100% sure about how to do this, so may not have done it correctly) but this didn't make a difference to the output.
As a 'what am I missing?' exercise, I tried installing Liberica 11 JDK on a fresh Lubuntu install - it previously had no Java version installed. before install, running java -version gave an error. After installing, java -version gives the same output as on MacOS (openJDK etc).
Is there actually a problem here (i.e. is OpenJDK still the active version on my Mac, does Liberica 11 JDK report the version to be OpenJDK?)
Any help gratefully received!
Sounds like you have both installed (OSX Yosemite iirc installs 1.8 by default) try adding 1.7 to your java --alternatives and or use the defined path (symlinks are lovely for this)
Try
which java
This should tell you where the executable is. I agree with linuxdev2013, it is likely that you have multiple versions installed. I imagine that the newer version is probably in your path.
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