JDK and macOS
JDK and macOS
Having trouble defining your JDK runtime on macOS? Java and Macs, am I right? Here's one solution to get you up and running via Terminal.
Join the DZone community and get the full member experience.Join For Free
Delivering modern software? Atomist automates your software delivery experience.
This is going to be short. I mean really short. Not even any diversions.
I mentioned in a recent post how "cross" I get with advice on setting JAVA_HOME on macOS. It is just not that complicated. I have seen many questions on StackOverflow and other forums...
- "How do I set JAVA_HOME on Mac?"
- "Where is Java installed on OSX?"
If you don't get a petulant response from someone too big to be polite, you'll get a myriad of suggestions.
- First rule; DON'T install anything other than the JDK, it's just not worth your while.
- Secondly, DON'T be afraid of making changes — you won't break anything fundamental.
So, you've installed JDK10 from Oracle, the newest and shiniest. Now, no older Java-based solutions function. So what do we do?!
Lets set the operating system's JAVA_HOME. We do that in the bash_profile. If it does not exist, create it. It doesn't even matter how, the only requirement is that it is hidden and that it is in the root directory of your user.
So, how do we do that? Open a terminal (I like iTerm):
I actually use Sublime Text myself, but whatever takes your fancy. If you do not have a bash_profile, then simply create one:
Now, which JDK(s) do you have installed? Again, it is not complicated:
You'll get an output similar to my own:
Now, let's check what JAVA_HOME is now:
1.8.0_171-b11 - whatever!!
Let's add a line to that bash_profile we created.
export JAVA_HOME=`/usr/libexec/java_home -v 10.0.1`
Notice that I got the version from the output of the "-V" command. Now, all we have to do is reinitialize the terminal.
From here on in, whenever an application runs Java, it is going to read JAVA_HOME, which is set by the bash_profile. To change it, you just follow the same process. I have been using this method of defining my runtime for some years.
I've not found a scenario where this solution does not cover the bases, but I am happy to hear stories.
Opinions expressed by DZone contributors are their own.