Mac OS X – Finding and setting JAVA_HOME

Finding all your Java installations on Windows and Linux is pretty straightforward. Tracking them down on a Mac is a touch more challenging.

If you have multiple Java versions installed on a Windows or Linux machine you can probably find them without a second’s thought. Unless you’ve specifically installed them into non-standard locations they’re almost certainly all clutched together under c:\program files or somewhere like /usr/java:-

[devg@localhost java]$ cd /usr/java
[devg@localhost java]$ ls -ltr
total 8
drwxr-xr-x. 7 root root 4096 Oct 19 2012 jdk1.6.0_37
drwxr-xr-x 7 root root 4096 Apr 24 21:25 jdk1.6.0_45

If you’ve only got one Java version installed on your Mac you might never need to worry where it’s hiding:-

localhost:~ devg$ which java
/usr/bin/java
localhost:~ devg$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

If you’ve got multiple versions installed and need to set the JAVA_HOME variable to switch between those versions, you might be scratching your head a little wondering where they’re hiding.

Well, you can always play your get-out-of-gaol find card:-

localhost:~ devg$ sudo find / -name "javac" -print
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/bin/javac
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/javac

And if you’re fortunate enough to be using a Java IDE such as Eclipse Luna it’ll find them for you:-

OS X Java Installations shown in Eclipse Luna

And, since those paths don’t exactly lend themselves to quick and easy recall when you’re knocking together a quick shell script or running a Java application from the command line, there’s a third way too – the java_home command.

Slightly unhelpfully java_home sits in /usr/libexec and is unlikely to be on your path by default:-

localhost:~ devg$ /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.

There are quite a few options here, a few of which are really useful. First of all the -V option will quickly list all the JVMs on your system and where they’re installed:-

localhost:~ devg$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
   1.8.0_11, x86_64:"Java SE 8"
     /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
   1.7.0_51, x86_64:"Java SE 7"
     /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
   1.6.0_65-b14-462, x86_64:     "Java SE 6"
     /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
   1.6.0_65-b14-462, i386:       "Java SE 6"
     /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

The other really useful option is -v which will return the JAVA_HOME for a specified Java version:-

localhost:~ devg$ /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

You can use this to set the JAVA_HOME that /usr/bin/java will pick up:-

localhost:~ devg$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
localhost:~ devg$ export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
localhost:~ devg$ java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

You can also use its -exec parameter to execute a java command with the appropriate JAVA_HOME in a single command:-

localhost:~ devg$ /usr/libexec/java_home -v 1.7 --exec java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

If you don’t have Java installed on your Mac, the -R parameter will automatically download it from Apple; however it will only pull down the latest Apple supported version and ignores the -v option:-

localhost:~ devg$ /usr/libexec//java_home -v 1.8 -R

OS X Automatic Java download dialogue

Unless it’s the current Apple released JDK for your OS X version that you’re after it’s probably best to head over to Oracle’s Java SE downloads page instead.

Leave a Reply

Your email address will not be published. Required fields are marked *