How to set JAVA_HOME on Mac OS X

Trying to run a Java application on your Mac and need to set your JAVA_HOME?  Follow the instructions below to quickly and easily do it:

  • Open Terminal
  • Confirm you have JDK by typing “which java”. It should show something like /usr/bin/java.
  • Check you have the needed version of Java, by typing “java -version”.
    • JAVA_HOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java.
    • For Mac OSX – it is /Library/Java/Home
  • Set JAVA_HOME using this command in Terminal: export JAVA_HOME=/Library/Java/Home
  • echo $JAVA_HOME on Terminal to confirm the path
  • You should now be able to run your application

Note that this sets JAVA_HOME only for this session. If you want it to persist, you will have to add the command to your ~/.profile file.  Below are instructions on how to accomplish this instead:

  • Open up (Applications >> Utilities >> Terminal)
  • Type: emacs .profile
  • add this to the end of the .profile file:

export JAVA_HOME;

  • Save and exit emacs (ctrl-x, ctrl-s; ctrl-x, ctrl-c)
  • Open a new Terminal window (cmd-n) and type: $JAVA_HOME/bin/java -version

If you see something like:
java version “1.5.0_16″
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

Then you’ve successfully set your JAVA_HOME environment variable to the binary stored in /Library/Java/Home/bin/java