191

How do you check if Java SDK is installed on a Mac?

Is there a command line for this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
angry kiwi
  • 10,730
  • 26
  • 115
  • 161

11 Answers11

262

javac -version in a terminal will do

  • 4
    I got "javac 1.6.0_37" is it equivalent of jdk 6 or 7? – angry kiwi Jan 12 '13 at 11:26
  • 12
    However, if Java isn't installed, a dialog box will appear telling you that Java needs to be installed, so this isn't a good option for scripts. – a paid nerd Jan 05 '16 at 18:15
  • 4
    If you get "The operation couldn’t be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java.", got to https://www.oracle.com/java/technologies/javase-downloads.html and download java jdk! – Delicia Fernandes Dec 10 '20 at 13:42
  • this doesn't show any sdk path; only shows java runtime version for me on macOS 12.1, is it because I didn't install the sdk? – dragonfly02 Mar 19 '22 at 02:56
  • To show the path where the java jdk installed, use command "which javac". Also regarding the version naming, Java 8 is Java 1.8, where 8 is the version number, and 1.8 is the version string – Arya Mohanan Aug 02 '23 at 08:18
107

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.
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
15

Type in a terminal:

which javac

It should show you something like

/usr/bin/javac
asgoth
  • 35,552
  • 12
  • 89
  • 98
10

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

/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"
ramzes2
  • 773
  • 5
  • 13
  • annnnnnd it returns -127 if `/usr/libexec/java_home` doesn't even exist... so this actually works both ways ;-) – Tom Hundt Jul 19 '23 at 16:20
3
  • 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.

Mohtasim
  • 111
  • 5
3

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: https://knasmueller.net/how-to-install-java-openjdk-15-on-macos-big-sur

  1. Download the .tar.gz file of the JDK on https://jdk.java.net/15/
  2. 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.

  1. run /usr/libexec/java_home -v15 and copy the output
  2. add this line to your .bash_profile or .zshrc file, depending on which shell you are using. You will probably have only one of these files existing in your home directory (~/.bash_profile or ~/.zshrc).
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home
  1. save the changes and make them effective right away by running: source ~/.bash_profile or source ~/.zshrc
  2. check that java is working - run java -v
Aleksandar
  • 3,558
  • 1
  • 39
  • 42
1

Just type javac. If it is installed you get usage information, otherwise it would just ask if you would like to install Java.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

On MAC find your JDK path by executing the command.

/usr/libexec/java_home
Amit Meena
  • 2,884
  • 2
  • 21
  • 33
1

If you want to only check Java is already installed or not

just type- java on the terminal

and if you want to know about the version as well then

type- java -version on the terminal

Result will be look like:

enter image description here

Alok
  • 24,880
  • 6
  • 40
  • 67
0

Make sure you correctly define the project's JDK and restart IntelliJ (full restart).

Luís Soares
  • 5,726
  • 4
  • 39
  • 66