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 Terminal.app (Applications >> Utilities >> Terminal)
  • Type: emacs .profile
  • add this to the end of the .profile file:

JAVA_HOME=/Library/Java/Home
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

2 thoughts on “How to set JAVA_HOME on Mac OS X

  1. Thank you so much for your help!!! It is helpful.
    However, when I type $JAVA_HOME/bin/java -version
    It shows:
    java version “1.6.0_45″
    Java(TM) SE Runtime Environment (build 1.6.0_45-b06-451-11M4406)
    Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01-451, mixed mode)

    There is no “sharing” at last, is there any mistake?

    Looking forward to your reply.
    Best regards.

  2. Jany, there shouldn’t be any issue with not have the “sharing” portion. From what you’ve posted, your JAVA_HOME should be appropriately set now!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>