How do you check if Java SDK is installed on a Mac?
Is there a command line for this?
19 Answers
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:
$ /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:
$ /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:
REQUESTED_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.
$ /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. 1Type in a terminal:
which javac It should show you something like
/usr/bin/javac 3Below command worked out pretty good:
javac -version I also manually verified by navigating to the Java Folder on my Mac
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk /usr/bin/java_home tool returns 1 if java not installed.
So you can check if java is installed by the next way:
/usr/libexec/java_home &> /dev/null && echo "installed" || echo "not installed" If you are on Mac OS Big Sur, then you probably have a messed up java installation. I found info on how to fix the issue with this article:
- Download the .tar.gz file of the JDK on
- Navigate to the download folder, and run these commands (move the .tar.gz file, extract it and remove it after extraction):
sudo mv openjdk-15.0.2_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines/ cd /Library/Java/JavaVirtualMachines/ sudo tar -xzf openjdk-15.0.2_osx-x64_bin.tar.gz sudo rm openjdk-15.0.2_osx-x64_bin.tar.gz Note: it might be 15.0.3 or higher, depending on the date of your download.
- run
/usr/libexec/java_home -v15and copy the output - add this line to your
.bash_profileor.zshrcfile, depending on which shell you are using. You will probably have only one of these files existing in your home directory (~/.bash_profileor~/.zshrc).
JAVA_HOME=/Library/Java/JavaVirtualMachines/ - save the changes and make them effective right away by running:
source ~/.bash_profileorsource ~/.zshrc - check that java is working - run
java -v
Just type javac. If it is installed you get usage information, otherwise it would just ask if you would like to install Java.
- Open terminal.
run command to see:
javac -version
Also you can verify manually by going to the specific location and then check. To do this run below command in the mac terminal
cd /Library/Java/JavaVirtualMachines/
Then run ls command in the terminal again. Now you can see the jdk version & package if exists in your computer.
Make sure you correctly define the project's JDK and restart IntelliJ (full restart).