How to set JAVA_HOME environment variable on Mac OS X 10.9?

islander_zero picture islander_zero · Apr 3, 2014 · Viewed 237.3k times · Source

I just purchased a brand new MacBook Pro.

This is my first MAC ever and I'm still trying to get the hang of navigating my way around.

Anyway, I'm also new to Java and I've been practicing on my Windows PC before it permanently died.

Now that I'm on this MAC, I installed my JDK and now I need to set the JAVA_HOME environment variable.

I have no idea what to do.

I tried following some of these guides and didn't get very far.

  1. Mkyong.com : How to set JAVA_HOME variable in Mac OSX

  2. YouTube : How to set environment variables on mac, linux, solaris, rhel

  3. YouTube : How to Set Environment Variables in Mac

I was able to locate the terminal and I think I created some multiple files. I'm getting messages like this:


(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.


(2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r /Users/Erwin/.bash_profile" to recover the changes (see ":help recovery"). If you did this already, delete the swap file "/Users/Erwin/.bash_profile.sw p" to avoid this message.

Can somebody tell how to set Java in Mac OSX environment step by step?

Answer

Adrian Petrescu picture Adrian Petrescu · Apr 3, 2014

Literally all you have to do is:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

and restart your shell.

If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:

echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile