Mac OS X – Set JAVA_HOME

While running Java applications, sometimes you may have to set JAVA_HOME environment variable. Here are the instructions to quickly set JAVA_HOME on your Mac OS X.

  1. Open Terminal
  2. To Confirm if you have Java
    • Type command: “which java”
    • Hit Enter/Return button
    • The command should return something like this: /usr/bin/java
  3. To get location of Java (detailed steps are documents here)
    • Type command: /usr/libexec/java_home
    • Hit Enter/Return button
    • The command should return something like this: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
  4. To Set JAVA_HOME for single session
    • Type command: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
    • Hit Enter/Return button
    • Type command to verify: echo $JAVA_HOME
  5. To Set JAVA_HOME permanently
    •  Type command: vi ~/.bash_profile
    • This will open bash_profile file
    • Go to the end, and add following lines
      • JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
      • export JAVA_HOME;
    • Save the file
    • Restart Terminal
    • Type command to verify: echo $JAVA_HOME

Leave a Reply

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