17

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 ?

Community
  • 1
  • 1
Andy
  • 2,493
  • 6
  • 37
  • 63

4 Answers4

37

Where to define it

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.

What do define

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
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • 1
    You did not answer the first question that was aked here. – Peter Verhas Jul 10 '16 at 09:50
  • @PeterVerhas Oh well, I did implicitly: It is not available since it was not exported in the proper dotfile. For an environment variable to be available, it needs to be set ;) – Markus W Mahlberg Jul 11 '16 at 09:03
  • FYI - This does not work on High Sierra. I needed to specify jdk1.8, and had java 9 installed. The only solution was to delete Java 9. – eimmer Dec 01 '17 at 16:31
  • @eimmer That does not stand against the basic answer to the question. Downgrading Java is a whole different thing. – Markus W Mahlberg Dec 02 '17 at 12:32
  • @MarkuswMahlberg This answer would not work for me on High Sierra, so I wanted to make people aware that this answer might not apply to them. I'm going to dig in some and see if I kind find out more... – eimmer Dec 05 '17 at 16:26
  • So, this does work work HighSierra, this doesn't work if your default terminal is ZSH... – eimmer Dec 06 '17 at 13:34
7
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

that is for Java 8 .... original link where I found it is here Installing Java on Mac OS X 10.10 Yosemite and 10.11 El Capitan

But the accepted answer works too. Just need to pay attention to path changes after updates.

Community
  • 1
  • 1
SDen
  • 191
  • 3
  • 5
2

You will find many answers on how to set JAVA_HOME in OSX but it is hard to find an answer to the question if setting JAVA_HOME is necessary at all.

Why is that? Does that mean that my JAVA_HOME is not set properly?

On OSX, setting the JAVA_HOME environment variable is optional. The JRE and JDK installers do not provide for JAVA_HOME being set; so JAVA_HOME not being set is perfectly fine.

chw1k
  • 186
  • 1
  • 5
0

If you want to use jdk 1.6 for some reason, which Apple has stopped supporting. It will be removed after you updated Mac OS. You can still get jdk 1.6 from this page: https://support.apple.com/kb/DL1572?locale=zh_CN

After you install jdk 1.6 again, remember to confirm exporting the correct directory for system variable $JAVA_HOME in ~/.bash_profile, etc. For me, it is "/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home".

Willie Z
  • 476
  • 7
  • 12