2

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?

  • No popup at all. $ java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode) – jherran Nov 6 '14 at 22:21
  • 1
    @jherran you didn't get a popup because Java is installed. If Java isn't installed on a machine yet (and it isn't by default in fresh OS X installs) you'll get a popup requesting to install it on-demand from Apple's servers. – Ian C. Nov 6 '14 at 23:35
1

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
  • 1
    I would add that /usr/libexec/java_home exists on Mavericks at least, but that running it gives a non-zero return-code, which one can then test for (as presumably the return-code is 0 when a Java is found) – Brian M. Hunt Nov 7 '14 at 0:07
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .