{"id":161677,"date":"2020-09-30T08:00:20","date_gmt":"2020-09-30T12:00:20","guid":{"rendered":"https:\/\/22f02bbc68.nxcli.net\/?p=161677"},"modified":"2023-11-01T13:13:42","modified_gmt":"2023-11-01T17:13:42","slug":"manage-java-versions-macos","status":"publish","type":"post","link":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/","title":{"rendered":"The Right Way to Install and Manage Java Versions on macOS"},"content":{"rendered":"<p>There is a correct way to install Java on macOS. If you <em><strong>don&#8217;t<\/strong><\/em> follow it, everything will probably work just fine&#8230; at least until you need to switch which version of Java you&#8217;re using.<\/p>\n<p>Unfortunately, it&#8217;s pretty common to want to specify a particular Java version for your project. For example, the version of Gradle used by React Native is incompatible with JDKs newer than 11.<\/p>\n<p>So instead of installing Java any old way, follow these instructions. You&#8217;ll thank me later.<\/p>\n<h2>1. Extract the JDK to the Correct Place<\/h2>\n<p>&nbsp;<\/p>\n<p>On macOS, the correct place to install a JDK is <code>\/Library\/Java\/JavaVirtualMachines<\/code>. Untar the JDK as-is into a subfolder of this.<\/p>\n<p>If you did this step right, you&#8217;ll have a folder named &#8220;jdk-11.0.7.jdk.&#8221; There should also be a Contents\/Home subfolder of that:<\/p>\n<p><code>\/Library\/Java\/JavaVirtualMachines\/jdk-11.0.7.jdk\/Contents\/Home<\/code><\/p>\n<h2>2. Export JAVA_HOME<\/h2>\n<p>The JDK has been extracted to the right place, but it&#8217;s still not quite ready to use. You&#8217;ve got to set the <code>JAVA_HOME<\/code> environment variable.<\/p>\n<p>Using the example above, it would look like this:<\/p>\n<pre><code class=\"language-java\">export\nJAVA_HOME=\"\/Library\/Java\/JavaVirtualMachines\/jdk-11.0.7.jdk\/Contents\/Home\"<\/code><\/pre>\n<p>And you&#8217;re done. macOS has built-in wrappers for Java commands that will first check this environment variable before trampolining over to the JDK specified by it.<\/p>\n<p>It&#8217;s really handy that this is set via an environment variable because it allows you to, in tandem with tools like direnv, easily switch between JDK versions depending on the project that you&#8217;re working on.<\/p>\n<h2>3. \/usr\/libexec\/java_home (Bonus Step)<\/h2>\n<p>&nbsp;<\/p>\n<p>macOS also has a utility for finding the location of a JDK. You can read about it via <code>man java_home<\/code>.<\/p>\n<p>The most useful component is that you can provide <code>java_home<\/code> a less precise version and let it figure out which specific path to use. This is really helpful if you&#8217;re setting a project-specific JDK and sharing this configuration with other team members via direnv.<\/p>\n<p>The above example could be rewritten using <code>java_home<\/code> as:<\/p>\n<pre><code>export JAVA_HOME=$(\/usr\/libexec\/java_home -v 11)<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>&nbsp;<\/p>\n<p>I&#8217;ve often seen weird problems arise on other developers&#8217; machines that result from how some old version of Java was installed. If you always follow this pattern, it should save you from some headaches.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a correct way to install Java on macOS. If you don&#8217;t follow it, everything will probably work just fine&#8230; at least until you need to switch which version of Java you&#8217;re using. Unfortunately, it&#8217;s pretty common to want to specify a particular Java version for your project. For example, the version of Gradle [&hellip;]<\/p>\n","protected":false},"author":455,"featured_media":153393,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1764],"tags":[],"series":[],"class_list":["post-161677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-dev"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The Right Way to Install &amp; Manage Java Versions on macOS<\/title>\n<meta name=\"description\" content=\"There is one correct way to install Java on macOS. If you don&#039;t follow it, everything will work... until you need to switch which version you&#039;re using.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Right Way to Install &amp; Manage Java Versions on macOS\" \/>\n<meta property=\"og:description\" content=\"There is one correct way to install Java on macOS. If you don&#039;t follow it, everything will work... until you need to switch which version you&#039;re using.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/\" \/>\n<meta property=\"og:site_name\" content=\"Atomic Spin\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/atomicobject\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-30T12:00:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-01T17:13:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"538\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Chris Farber\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@atomicobject\" \/>\n<meta name=\"twitter:site\" content=\"@atomicobject\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chris Farber\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/\"},\"author\":{\"name\":\"Chris Farber\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#\\\/schema\\\/person\\\/3c7d1972f93a6f4d2b21f597f94f4cac\"},\"headline\":\"The Right Way to Install and Manage Java Versions on macOS\",\"datePublished\":\"2020-09-30T12:00:20+00:00\",\"dateModified\":\"2023-11-01T17:13:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/\"},\"wordCount\":362,\"publisher\":{\"@id\":\"https:\\\/\\\/atomicobject.com\\\/\"},\"image\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/Java.png\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/\",\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/\",\"name\":\"The Right Way to Install & Manage Java Versions on macOS\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/Java.png\",\"datePublished\":\"2020-09-30T12:00:20+00:00\",\"dateModified\":\"2023-11-01T17:13:42+00:00\",\"description\":\"There is one correct way to install Java on macOS. If you don't follow it, everything will work... until you need to switch which version you're using.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/manage-java-versions-macos\\\/#primaryimage\",\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/Java.png\",\"contentUrl\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/Java.png\",\"width\":1000,\"height\":538},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#website\",\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/\",\"name\":\"Atomic Spin\",\"description\":\"Atomic Object\u2019s blog on everything we find fascinating.\",\"publisher\":{\"@id\":\"https:\\\/\\\/atomicobject.com\\\/\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/spin.atomicobject.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#organization\",\"name\":\"Atomic Object\",\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/AO-Logo-Emblem-Color.png\",\"contentUrl\":\"https:\\\/\\\/spin.atomicobject.com\\\/wp-content\\\/uploads\\\/AO-Logo-Emblem-Color.png\",\"width\":258,\"height\":244,\"caption\":\"Atomic Object\"},\"image\":{\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/atomicobject\",\"https:\\\/\\\/x.com\\\/atomicobject\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/spin.atomicobject.com\\\/#\\\/schema\\\/person\\\/3c7d1972f93a6f4d2b21f597f94f4cac\",\"name\":\"Chris Farber\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg\",\"caption\":\"Chris Farber\"},\"url\":\"https:\\\/\\\/spin.atomicobject.com\\\/author\\\/farber\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Right Way to Install & Manage Java Versions on macOS","description":"There is one correct way to install Java on macOS. If you don't follow it, everything will work... until you need to switch which version you're using.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/","og_locale":"en_US","og_type":"article","og_title":"The Right Way to Install & Manage Java Versions on macOS","og_description":"There is one correct way to install Java on macOS. If you don't follow it, everything will work... until you need to switch which version you're using.","og_url":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/","og_site_name":"Atomic Spin","article_publisher":"https:\/\/www.facebook.com\/atomicobject","article_published_time":"2020-09-30T12:00:20+00:00","article_modified_time":"2023-11-01T17:13:42+00:00","og_image":[{"width":1000,"height":538,"url":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png","type":"image\/png"}],"author":"Chris Farber","twitter_card":"summary_large_image","twitter_creator":"@atomicobject","twitter_site":"@atomicobject","twitter_misc":{"Written by":"Chris Farber","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/#article","isPartOf":{"@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/"},"author":{"name":"Chris Farber","@id":"https:\/\/spin.atomicobject.com\/#\/schema\/person\/3c7d1972f93a6f4d2b21f597f94f4cac"},"headline":"The Right Way to Install and Manage Java Versions on macOS","datePublished":"2020-09-30T12:00:20+00:00","dateModified":"2023-11-01T17:13:42+00:00","mainEntityOfPage":{"@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/"},"wordCount":362,"publisher":{"@id":"https:\/\/atomicobject.com\/"},"image":{"@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/#primaryimage"},"thumbnailUrl":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png","articleSection":["Java"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/","url":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/","name":"The Right Way to Install & Manage Java Versions on macOS","isPartOf":{"@id":"https:\/\/spin.atomicobject.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/#primaryimage"},"image":{"@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/#primaryimage"},"thumbnailUrl":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png","datePublished":"2020-09-30T12:00:20+00:00","dateModified":"2023-11-01T17:13:42+00:00","description":"There is one correct way to install Java on macOS. If you don't follow it, everything will work... until you need to switch which version you're using.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/spin.atomicobject.com\/manage-java-versions-macos\/#primaryimage","url":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png","contentUrl":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/Java.png","width":1000,"height":538},{"@type":"WebSite","@id":"https:\/\/spin.atomicobject.com\/#website","url":"https:\/\/spin.atomicobject.com\/","name":"Atomic Spin","description":"Atomic Object\u2019s blog on everything we find fascinating.","publisher":{"@id":"https:\/\/atomicobject.com\/"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/spin.atomicobject.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/spin.atomicobject.com\/#organization","name":"Atomic Object","url":"https:\/\/spin.atomicobject.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/spin.atomicobject.com\/#\/schema\/logo\/image\/","url":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/AO-Logo-Emblem-Color.png","contentUrl":"https:\/\/spin.atomicobject.com\/wp-content\/uploads\/AO-Logo-Emblem-Color.png","width":258,"height":244,"caption":"Atomic Object"},"image":{"@id":"https:\/\/spin.atomicobject.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/atomicobject","https:\/\/x.com\/atomicobject"]},{"@type":"Person","@id":"https:\/\/spin.atomicobject.com\/#\/schema\/person\/3c7d1972f93a6f4d2b21f597f94f4cac","name":"Chris Farber","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/217895b63a9f8ae1b67495e104c8f31f678405d712f69fee875f37b6d4658314?s=96&d=blank&r=pg","caption":"Chris Farber"},"url":"https:\/\/spin.atomicobject.com\/author\/farber\/"}]}},"_links":{"self":[{"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/posts\/161677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/users\/455"}],"replies":[{"embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/comments?post=161677"}],"version-history":[{"count":0,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/posts\/161677\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/media\/153393"}],"wp:attachment":[{"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/media?parent=161677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/categories?post=161677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/tags?post=161677"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/spin.atomicobject.com\/wp-json\/wp\/v2\/series?post=161677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}