setting JAVA_HOME to the JDK location mac osx 10.9.5

4.5k views Asked by At

I followed this thread to solve my problem, I have the same problem with a different version of JDK and for some reason it didn't work. I still get -bash: JAVA_HOME: command not found when I type JAVA_HOME to my terminal. I have JDK1.8.0.45 located at Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home which I can access with /usr/libexec/java_home

Using

echo "export JAVA_HOME=/usr/libexec/java_home" >> ~/.profile

~/.profile

I do get "export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home" saved to ~/.profile, and I can check this using /usr/bin/open ~/.profile

But I do not understand why JAVA_HOME still doesn't work and I need this to install maven.

Thanks!

2

There are 2 answers

0
Firefly On BEST ANSWER

My ~/.bash_profile has this line in it, and it's been working fine.

export JAVA_HOME=$(/usr/libexec/java_home)

This line basically says "run the program named /usr/libexec/java_home and export its output as a variable named JAVA_HOME."

I suggest opening your .profile or .bash_profile in a text editor rather than using echo statements to append new lines to it. That way you can see everything that's in the file and make sure other old lines in the file aren't causing you issues.

After you make a change to .bash_profile, make sure you open a new terminal window before testing it.

You can check the value of any environment variable (including JAVA_HOME) by simply echo'ing its value:

echo $JAVA_HOME

In my case the output of that echo command is: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home

0
Michał Kalinowski On

Probably you misunderstood things. JAVA_HOME is not a command, it's an environment variable. You can't call JAVA_HOME in your terminal since - in fact - there is no such command, what your error message says.

You can see this variable's value by:

echo $JAVA_HOME

If it is set and points to a proper location then it's fine. Some tools that expect this variable to be set (e.g. Maven or Java IDEs) will work well.