I am trying to run a JAVA application on my MAC OS X 10.11 ( El Capitan).
I used this link to set the JAVA_HOME on my MAC OS X 10.11.
I am able to run the following commands:
java -version
which java
However when I go to the Terminal app on MAC and type echo $JAVA_HOME, I see nothing. Why is that? Does that mean that my JAVA_HOME is not set properly ?
The JAVA_HOME environment variable points to the file system location where the JDK or JRE was installed in your system. You can specify the path according to the location where Java is installed in your system. For MAC OSX it is: /Library/Java/Home. export JAVA_HOME=/Library/Java/Home.
Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly.
Ok, first of all, we have to make clear where to set JAVA_HOME
.
Simplified, you can define it in two files: either ~/.bashrc
or ~/bash_profile
. By default the former is executed for what is called "interactive non-login shells" while the latter is used for "login shells".
A "login shell" is exactly what you'd expect: a shell which is started after login via command line. An "interactive non-login shell" is a shell which is started from within a GUI for example. So, according to that, we should put our export statement into ~/.bashrc
.
Side note: While OS X's "Terminal" application reads both files mentioned, this is not the default behavior and therefor should not be treated as such. And thats why I wrote an explanation.
You need to export JAVA_HOME in the ~/.bashrc
file so that every time a shell is opened, the variable is set.
On OS X, the Java Development Kits and Runtime Environments are stored under /Library/Java/JavaVirtualMachines/
for quite a while now. Have a look there. This is how it looks at my machine:
/Library/Java/JavaVirtualMachines/
├── jdk1.7.0_45.jdk
├── jdk1.8.0_20.jdk
├── jdk1.8.0_25.jdk
└── jdk1.8.0_51.jdk
The subfolders look similar to this
jdk1.7.0_45.jdk/
└── Contents
├── Home
├── Info.plist
└── MacOS
And there we got it. So if you wanted to point to the JDK 1.7.0_45, you'd put the following statement into your .bashrc
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home"
To make sure that the correct binaries for that Java version are called, you also should add the following somewhere after the above statement:
export PATH=$JAVA_HOME/bin:$PATH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With