islander_zero islander_zero - 10 months ago 62
Java Question

Setting JAVA_HOME environment variable on MAC OSX 10.9

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. Anyways... 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.

And I'm very sure this one made me create a now useless file:

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
to avoid this message.

Can some Mac expert out there help me in fixing this and setting Java environment step by step?

Answer Source

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