DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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.

Chris Luff user avatar by
Chris Luff
·
May. 17, 18 · Java Zone · Tutorial
Like (26)
Save
Tweet
19.45K Views

Join the DZone community and get the full member experience.

Join For Free

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):

open ~/.bash_profile


I actually use Sublime Text myself, but whatever takes your fancy. If you do not have a bash_profile, then simply create one:

cat ~/.bash_profile


Now, which JDK(s) do you have installed? Again, it is not complicated:

/usr/libexec/java_home -V


You'll get an output similar to my own:

Image title

Now, let's check what JAVA_HOME is now:

java -version

Image title

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.

source .bash_profile


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.

Java (programming language) Java Development Kit MacOS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Optional in Java: When Better Code Is Not an Alternative
  • DevOps Security Checklist for Kubernetes
  • It's Official! Fat Arrows in JavaScript!
  • Fun With Modules

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo