How does one detect if java is installed without triggering a visual prompt?

On most systems, one would just run java -version and a non-zero return code would indicate that Java is not installed.

One could do this on OS X, but a nuisance-prompt comes up to ask the user if they want to install Java.

This is irritating in automated scripts.

How can one detect whether Java is installed on a Mac, without getting the popup?


All the most recent Java's install /usr/libexec/java_home which is used to select between multiple Java installations on a machine. Testing for its existence and the output from the command are good ways to see if Java is installed without triggering the pop up.

if [[ -e /usr/libexec/java_home ]]; then
    JAVA_HOME=$(/usr/libexec/java_home)
else
    echo "Java not installed"
fi

You can also use it to look for a specific version of Java. For example:

|ruby-2.1.1| cortana in ~
○ → /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

|ruby-2.1.1| cortana in ~
○ → /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

|ruby-2.1.1| cortana in ~
○ → /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home

rtrouton has some great examples on his derflounder website. The examples are used as extension attributes in a mdm suite, just remove the results tags from them. Below is a script that looks for the java install folder, if it exists echo out the version if not echo not installed. This in return never triggers the java command keeping the pop up away.

if [[ -e /Library/Java/Home ]]; then
    echo "$(java -version 2>&1 | awk '/version/{print $3}' | sed 's/"//g')"
        else
    echo "Java not installed"
fi