How do I install Java on Mac OSX allowing version switching?












86















I want to install OpenJDK Java 11 on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.



The only other installs I found that do more things automatically are the Oracle install via Homebrew cask, but that is the commercial JDK and I want the open-source one!



brew cask info java


Shows:




java: 11,28:55eed80b163941c8885ad9298e6d786a
https://www.oracle.com/technetwork/java/javase/overview/index.html




Which it is the Oracle JDK and not OpenJDK, see:
https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712



Following directions from an older question (Mac OS X and multiple Java versions) is outdated and gives me the wrong version -- Oracle not OpenJDK.



Update: As of October 3, 2018 the Homebrew cask is now pointing to OpenJDK 11










share|improve this question

























  • Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

    – nullpointer
    Nov 15 '18 at 15:20


















86















I want to install OpenJDK Java 11 on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.



The only other installs I found that do more things automatically are the Oracle install via Homebrew cask, but that is the commercial JDK and I want the open-source one!



brew cask info java


Shows:




java: 11,28:55eed80b163941c8885ad9298e6d786a
https://www.oracle.com/technetwork/java/javase/overview/index.html




Which it is the Oracle JDK and not OpenJDK, see:
https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712



Following directions from an older question (Mac OS X and multiple Java versions) is outdated and gives me the wrong version -- Oracle not OpenJDK.



Update: As of October 3, 2018 the Homebrew cask is now pointing to OpenJDK 11










share|improve this question

























  • Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

    – nullpointer
    Nov 15 '18 at 15:20
















86












86








86


50






I want to install OpenJDK Java 11 on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.



The only other installs I found that do more things automatically are the Oracle install via Homebrew cask, but that is the commercial JDK and I want the open-source one!



brew cask info java


Shows:




java: 11,28:55eed80b163941c8885ad9298e6d786a
https://www.oracle.com/technetwork/java/javase/overview/index.html




Which it is the Oracle JDK and not OpenJDK, see:
https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712



Following directions from an older question (Mac OS X and multiple Java versions) is outdated and gives me the wrong version -- Oracle not OpenJDK.



Update: As of October 3, 2018 the Homebrew cask is now pointing to OpenJDK 11










share|improve this question
















I want to install OpenJDK Java 11 on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.



The only other installs I found that do more things automatically are the Oracle install via Homebrew cask, but that is the commercial JDK and I want the open-source one!



brew cask info java


Shows:




java: 11,28:55eed80b163941c8885ad9298e6d786a
https://www.oracle.com/technetwork/java/javase/overview/index.html




Which it is the Oracle JDK and not OpenJDK, see:
https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712



Following directions from an older question (Mac OS X and multiple Java versions) is outdated and gives me the wrong version -- Oracle not OpenJDK.



Update: As of October 3, 2018 the Homebrew cask is now pointing to OpenJDK 11







java java-11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 4:29







Jayson Minard

















asked Sep 26 '18 at 18:32









Jayson MinardJayson Minard

39.5k17109173




39.5k17109173













  • Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

    – nullpointer
    Nov 15 '18 at 15:20





















  • Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

    – nullpointer
    Nov 15 '18 at 15:20



















Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

– nullpointer
Nov 15 '18 at 15:20







Just asking, can we keep the question tags restricted to a single version? Since there is no such version-specific installation script/steps anyway. This is a general(non-version specific) question and can suffice to be tagged with only java-11, since I assume that is the place where people would start looking out for what the question and answer suggests.

– nullpointer
Nov 15 '18 at 15:20














5 Answers
5






active

oldest

votes


















191














note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, and even early access Java 12!



You have a few options of how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting JAVA_HOME. All of these are described below.







Installation



First, install Java using whatever method you prefer including Homebrew, SDKMAN or a manual install of the tar.gz file. The advantages of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.



Install with SDKMAN



This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.



<see below "Installing and Switching versions with SDKMAN">



Install using Jabba



This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.



<see below "Installing and Switching versions with Jabba">



Install manually from OpenJDK download page:




  1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/ (or early access JDK 12 from http://jdk.java.net/12/)


  2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.



Install with Homebrew



The version of Java available in Homebrew Cask previos to October 3, 2018 was indeed the Oracle JVM. Now however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.





  1. install Homebrew if you haven't already. Make sure it is updated:



    brew update



  2. Add the casks tap, if you haven't already:



    brew tap homebrew/cask-versions



  3. Look for installable versions:



    brew search java



  4. Check the details on the version that will be installed:



    brew cask info java


    Note: prior to October 3, 2018 that the current release is the Oracle commercial version of Java 11, and after October 3 this is now OpenJDK 11.




  5. Install a specific version of the JDK such as java8, java10 or java for the current:



    brew cask install java


    The AdoptOpenJDK version is available as well now from Homebrew Cask:



    brew cask install adoptopenjdk



And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.



Other installation options:



Some other flavours of openJDK are:



Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.




Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.




Amazon Correto OpenJDK builds have an easy to use installation package for version 8 (other versions are coming), and installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.




Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Amazon Linux 2, Windows, and macOS.






Where is my JDK?!?!



To find locations of previously installed Java JDK's installed at the default system locations, use:



/usr/libexec/java_home -V



Matching Java Virtual Machines (8):
11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home




You can also report just the location of a specific Java version using -v



/usr/libexec/java_home -v 11



/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home




Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba -- and you need to know where to find them.



If you need to find JDK's instaled by other tools, check these locations:




  • SDKMAN installs to ~/.sdkman/candidates/java/

  • Jabba installs to ~/.jabba/jdk




Switching versions manually



The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.



For example, if you installed or untar'd JDK 11 to /Library/Java/JavaVirtualMachines/jdk-11.jdk if it is the highest version number it should already be the default, if not you could simply set:



export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


And now whatever Java executable is in the path will see this and use the correct JDK.



Using the /usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your .profile or .bash_profile to change JAVA_HOME for you:



export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
alias java10='export JAVA_HOME=$JAVA_10_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'

# default to Java 11
java11


Then to change versions, just use the alias.



java8
java -version



java version "1.8.0_144"




Of course setting JAVA_HOME manually works too!





Switching versions with JEnv



JEnv expects the Java JDK's to already exist on the machine and can be in any location. Typically you will find installed Java JDK's in /Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.




  1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.



  2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):



    jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home



  3. Set your global version using this command:



    jenv global 11



You can also add other existing versions using jenv add in a similar manner, and list those that are available. For example Java 8:



jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 
jenv versions


See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.



To help manage JAVA_HOME while using JEnv you can add the export plugin to do this for you.



$ jenv enable-plugin export
You may restart your session to activate jenv export plugin echo export plugin activated


The export plugin may not adjust JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.



You can also use jenv exec <command> <parms...> to run single commands with JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.





Installing and Switching versions with SDKMAN



SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.




  1. Install SDKMAN from https://sdkman.io/install



  2. Install Java 11 using SDKMAN:



    sdk install java 11.0.0-open 



  3. Make 11 the default version:



    sdk default java 11.0.0-open


    Or switch to 11 for the session:



    sdk use java 11.0.0-open



You can list available versions for installation using the list command:



sdk list java


And install additional versions, such as JDK 8:



sdk install java 8.0.181-oracle


SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:



sdk install java my-local-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


And use it freely:



sdk use java my-local-11


More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.



SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.





Installing and Switching versions with Jabba



Jabba also handles both the install and the switching. Jabba also places the installed JDK's into its own directory tree, which is typically ~/.jabba/jdk.




  1. Install Jabba by following the instructions on the home page.



  2. List available JDK's



    jabba ls-remote



  3. Install Java JDK 11



    jabba install openjdk@1.11.0



  4. Use it:



    jabba use openjdk@1.11.0



You can also alias version names, link to existing JDK's already installed, and find a mix of interesting JDK's such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.



Jabba will automatically manage your PATH and JAVA_HOME for you as you change versions.






share|improve this answer





















  • 2





    For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

    – Peter Hull
    Sep 27 '18 at 8:20











  • @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

    – Jayson Minard
    Sep 29 '18 at 6:39











  • quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

    – Peter Hull
    Sep 30 '18 at 17:52






  • 2





    Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

    – Xingang Huang
    Oct 4 '18 at 17:30






  • 4





    Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

    – Kevin Hooke
    Oct 20 '18 at 19:44



















2














Manually switching system-default version without 3rd party tools:



As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.



Armed with this knowledge you can:




  • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).

  • control which version to use for specific tasks by setting $JAVA_HOME


I've just verified this is still true with OpenJDK & Mojave.



On a brand new system, there is no Java version installed:



$ java -version
No Java runtime present, requesting install.


Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
install OpenJDK11:



$ cd /Library/Java/JavaVirtualMachines/
$ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz


System java is now 11:



$ java -version
openjdk version "11.0.1" 2018-10-16
[...]


Install OpenJDK12 (early access at the moment):



$ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 


System java is now 12:



$ java -version
openjdk version "12-ea" 2019-03-19
[...]


Now let's "hide" OpenJDK 12 from system java wrapper:



$ cd jdk-12.jdk/Contents/
$ sudo mv Info.plist Info.plist.disabled


System java is back to 11:



$ java -version
openjdk version "11.0.1" 2018-10-16
[...]


And you can still use version 12 punctually by manually setting JAVA_HOME:



$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
$ java -version
openjdk version "12-ea" 2019-03-19
[...]





share|improve this answer
























  • and it should also be true now with AdoptOpenJDK builds

    – Hugues M.
    Oct 30 '18 at 21:29



















1














This is how I did it.



Step 1: Install Java 11



You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html



Step 2: After installation of Java 11. Confirm installation of all versions. Type the following command in your terminal.



/usr/libexec/java_home -V


Step 3: Edit .bash_profile



sudo nano ~/.bash_profile


Step 4: Add 11.0.1 as default. (Add below line to bash_profile file).



export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)


to switch to any version



export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)


Now Press CTRL+X to exit the bash. Press 'Y' to save changes.



Step 5: Reload bash_profile



source ~/.bash_profile


Step 6: Confirm current version of Java



java -version





share|improve this answer

































    0














    Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/



    First install SDKMAN: https://sdkman.io/install and then...




    1. Install Oracle JDK 8 with: sdk install java 8.0.181-oracle

    2. Install OpenJDK 11 with: sdk install java 11.0.0-open


    To switch:




    • Switch to JDK 8 with sdk use java 8.0.181-oracle

    • Switch to JDK 11 with sdk use java 11.0.0-open


    To set a default:




    • Default to JDK 8 with sdk default java 8.0.181-oracle

    • Default to JDK 11 with sdk default java 11.0.0-open






    share|improve this answer

































      -1














      IMHO, There is no need to install all the additional applications/packages.



      Check available versions using the command:



      > /usr/libexec/java_home -V
      Matching Java Virtual Machines (8):
      11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
      9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
      1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
      1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
      1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
      1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
      1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


      Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:



      > /usr/libexec/java_home -v 1.8.0_181
      /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home


      To pick Oracle's Java SE 8 you would invoke the command:



      > /usr/libexec/java_home -v 1.8.0_151
      /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home


      As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151






      share|improve this answer


























      • Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

        – Jayson Minard
        Oct 7 '18 at 11:37











      • This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

        – Jayson Minard
        Oct 7 '18 at 12:01











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52524112%2fhow-do-i-install-java-on-mac-osx-allowing-version-switching%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      191














      note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, and even early access Java 12!



      You have a few options of how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting JAVA_HOME. All of these are described below.







      Installation



      First, install Java using whatever method you prefer including Homebrew, SDKMAN or a manual install of the tar.gz file. The advantages of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.



      Install with SDKMAN



      This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.



      <see below "Installing and Switching versions with SDKMAN">



      Install using Jabba



      This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.



      <see below "Installing and Switching versions with Jabba">



      Install manually from OpenJDK download page:




      1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/ (or early access JDK 12 from http://jdk.java.net/12/)


      2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.



      Install with Homebrew



      The version of Java available in Homebrew Cask previos to October 3, 2018 was indeed the Oracle JVM. Now however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.





      1. install Homebrew if you haven't already. Make sure it is updated:



        brew update



      2. Add the casks tap, if you haven't already:



        brew tap homebrew/cask-versions



      3. Look for installable versions:



        brew search java



      4. Check the details on the version that will be installed:



        brew cask info java


        Note: prior to October 3, 2018 that the current release is the Oracle commercial version of Java 11, and after October 3 this is now OpenJDK 11.




      5. Install a specific version of the JDK such as java8, java10 or java for the current:



        brew cask install java


        The AdoptOpenJDK version is available as well now from Homebrew Cask:



        brew cask install adoptopenjdk



      And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.



      Other installation options:



      Some other flavours of openJDK are:



      Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.




      Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.




      Amazon Correto OpenJDK builds have an easy to use installation package for version 8 (other versions are coming), and installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.




      Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Amazon Linux 2, Windows, and macOS.






      Where is my JDK?!?!



      To find locations of previously installed Java JDK's installed at the default system locations, use:



      /usr/libexec/java_home -V



      Matching Java Virtual Machines (8):
      11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
      9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
      1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home




      You can also report just the location of a specific Java version using -v



      /usr/libexec/java_home -v 11



      /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home




      Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba -- and you need to know where to find them.



      If you need to find JDK's instaled by other tools, check these locations:




      • SDKMAN installs to ~/.sdkman/candidates/java/

      • Jabba installs to ~/.jabba/jdk




      Switching versions manually



      The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.



      For example, if you installed or untar'd JDK 11 to /Library/Java/JavaVirtualMachines/jdk-11.jdk if it is the highest version number it should already be the default, if not you could simply set:



      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And now whatever Java executable is in the path will see this and use the correct JDK.



      Using the /usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your .profile or .bash_profile to change JAVA_HOME for you:



      export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
      export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
      export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
      export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

      alias java8='export JAVA_HOME=$JAVA_8_HOME'
      alias java9='export JAVA_HOME=$JAVA_9_HOME'
      alias java10='export JAVA_HOME=$JAVA_10_HOME'
      alias java11='export JAVA_HOME=$JAVA_11_HOME'

      # default to Java 11
      java11


      Then to change versions, just use the alias.



      java8
      java -version



      java version "1.8.0_144"




      Of course setting JAVA_HOME manually works too!





      Switching versions with JEnv



      JEnv expects the Java JDK's to already exist on the machine and can be in any location. Typically you will find installed Java JDK's in /Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.




      1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.



      2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):



        jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home



      3. Set your global version using this command:



        jenv global 11



      You can also add other existing versions using jenv add in a similar manner, and list those that are available. For example Java 8:



      jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 
      jenv versions


      See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.



      To help manage JAVA_HOME while using JEnv you can add the export plugin to do this for you.



      $ jenv enable-plugin export
      You may restart your session to activate jenv export plugin echo export plugin activated


      The export plugin may not adjust JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.



      You can also use jenv exec <command> <parms...> to run single commands with JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.





      Installing and Switching versions with SDKMAN



      SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.




      1. Install SDKMAN from https://sdkman.io/install



      2. Install Java 11 using SDKMAN:



        sdk install java 11.0.0-open 



      3. Make 11 the default version:



        sdk default java 11.0.0-open


        Or switch to 11 for the session:



        sdk use java 11.0.0-open



      You can list available versions for installation using the list command:



      sdk list java


      And install additional versions, such as JDK 8:



      sdk install java 8.0.181-oracle


      SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:



      sdk install java my-local-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And use it freely:



      sdk use java my-local-11


      More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.



      SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.





      Installing and Switching versions with Jabba



      Jabba also handles both the install and the switching. Jabba also places the installed JDK's into its own directory tree, which is typically ~/.jabba/jdk.




      1. Install Jabba by following the instructions on the home page.



      2. List available JDK's



        jabba ls-remote



      3. Install Java JDK 11



        jabba install openjdk@1.11.0



      4. Use it:



        jabba use openjdk@1.11.0



      You can also alias version names, link to existing JDK's already installed, and find a mix of interesting JDK's such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.



      Jabba will automatically manage your PATH and JAVA_HOME for you as you change versions.






      share|improve this answer





















      • 2





        For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

        – Peter Hull
        Sep 27 '18 at 8:20











      • @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

        – Jayson Minard
        Sep 29 '18 at 6:39











      • quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

        – Peter Hull
        Sep 30 '18 at 17:52






      • 2





        Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

        – Xingang Huang
        Oct 4 '18 at 17:30






      • 4





        Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

        – Kevin Hooke
        Oct 20 '18 at 19:44
















      191














      note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, and even early access Java 12!



      You have a few options of how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting JAVA_HOME. All of these are described below.







      Installation



      First, install Java using whatever method you prefer including Homebrew, SDKMAN or a manual install of the tar.gz file. The advantages of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.



      Install with SDKMAN



      This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.



      <see below "Installing and Switching versions with SDKMAN">



      Install using Jabba



      This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.



      <see below "Installing and Switching versions with Jabba">



      Install manually from OpenJDK download page:




      1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/ (or early access JDK 12 from http://jdk.java.net/12/)


      2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.



      Install with Homebrew



      The version of Java available in Homebrew Cask previos to October 3, 2018 was indeed the Oracle JVM. Now however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.





      1. install Homebrew if you haven't already. Make sure it is updated:



        brew update



      2. Add the casks tap, if you haven't already:



        brew tap homebrew/cask-versions



      3. Look for installable versions:



        brew search java



      4. Check the details on the version that will be installed:



        brew cask info java


        Note: prior to October 3, 2018 that the current release is the Oracle commercial version of Java 11, and after October 3 this is now OpenJDK 11.




      5. Install a specific version of the JDK such as java8, java10 or java for the current:



        brew cask install java


        The AdoptOpenJDK version is available as well now from Homebrew Cask:



        brew cask install adoptopenjdk



      And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.



      Other installation options:



      Some other flavours of openJDK are:



      Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.




      Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.




      Amazon Correto OpenJDK builds have an easy to use installation package for version 8 (other versions are coming), and installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.




      Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Amazon Linux 2, Windows, and macOS.






      Where is my JDK?!?!



      To find locations of previously installed Java JDK's installed at the default system locations, use:



      /usr/libexec/java_home -V



      Matching Java Virtual Machines (8):
      11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
      9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
      1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home




      You can also report just the location of a specific Java version using -v



      /usr/libexec/java_home -v 11



      /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home




      Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba -- and you need to know where to find them.



      If you need to find JDK's instaled by other tools, check these locations:




      • SDKMAN installs to ~/.sdkman/candidates/java/

      • Jabba installs to ~/.jabba/jdk




      Switching versions manually



      The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.



      For example, if you installed or untar'd JDK 11 to /Library/Java/JavaVirtualMachines/jdk-11.jdk if it is the highest version number it should already be the default, if not you could simply set:



      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And now whatever Java executable is in the path will see this and use the correct JDK.



      Using the /usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your .profile or .bash_profile to change JAVA_HOME for you:



      export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
      export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
      export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
      export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

      alias java8='export JAVA_HOME=$JAVA_8_HOME'
      alias java9='export JAVA_HOME=$JAVA_9_HOME'
      alias java10='export JAVA_HOME=$JAVA_10_HOME'
      alias java11='export JAVA_HOME=$JAVA_11_HOME'

      # default to Java 11
      java11


      Then to change versions, just use the alias.



      java8
      java -version



      java version "1.8.0_144"




      Of course setting JAVA_HOME manually works too!





      Switching versions with JEnv



      JEnv expects the Java JDK's to already exist on the machine and can be in any location. Typically you will find installed Java JDK's in /Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.




      1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.



      2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):



        jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home



      3. Set your global version using this command:



        jenv global 11



      You can also add other existing versions using jenv add in a similar manner, and list those that are available. For example Java 8:



      jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 
      jenv versions


      See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.



      To help manage JAVA_HOME while using JEnv you can add the export plugin to do this for you.



      $ jenv enable-plugin export
      You may restart your session to activate jenv export plugin echo export plugin activated


      The export plugin may not adjust JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.



      You can also use jenv exec <command> <parms...> to run single commands with JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.





      Installing and Switching versions with SDKMAN



      SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.




      1. Install SDKMAN from https://sdkman.io/install



      2. Install Java 11 using SDKMAN:



        sdk install java 11.0.0-open 



      3. Make 11 the default version:



        sdk default java 11.0.0-open


        Or switch to 11 for the session:



        sdk use java 11.0.0-open



      You can list available versions for installation using the list command:



      sdk list java


      And install additional versions, such as JDK 8:



      sdk install java 8.0.181-oracle


      SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:



      sdk install java my-local-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And use it freely:



      sdk use java my-local-11


      More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.



      SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.





      Installing and Switching versions with Jabba



      Jabba also handles both the install and the switching. Jabba also places the installed JDK's into its own directory tree, which is typically ~/.jabba/jdk.




      1. Install Jabba by following the instructions on the home page.



      2. List available JDK's



        jabba ls-remote



      3. Install Java JDK 11



        jabba install openjdk@1.11.0



      4. Use it:



        jabba use openjdk@1.11.0



      You can also alias version names, link to existing JDK's already installed, and find a mix of interesting JDK's such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.



      Jabba will automatically manage your PATH and JAVA_HOME for you as you change versions.






      share|improve this answer





















      • 2





        For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

        – Peter Hull
        Sep 27 '18 at 8:20











      • @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

        – Jayson Minard
        Sep 29 '18 at 6:39











      • quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

        – Peter Hull
        Sep 30 '18 at 17:52






      • 2





        Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

        – Xingang Huang
        Oct 4 '18 at 17:30






      • 4





        Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

        – Kevin Hooke
        Oct 20 '18 at 19:44














      191












      191








      191







      note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, and even early access Java 12!



      You have a few options of how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting JAVA_HOME. All of these are described below.







      Installation



      First, install Java using whatever method you prefer including Homebrew, SDKMAN or a manual install of the tar.gz file. The advantages of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.



      Install with SDKMAN



      This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.



      <see below "Installing and Switching versions with SDKMAN">



      Install using Jabba



      This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.



      <see below "Installing and Switching versions with Jabba">



      Install manually from OpenJDK download page:




      1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/ (or early access JDK 12 from http://jdk.java.net/12/)


      2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.



      Install with Homebrew



      The version of Java available in Homebrew Cask previos to October 3, 2018 was indeed the Oracle JVM. Now however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.





      1. install Homebrew if you haven't already. Make sure it is updated:



        brew update



      2. Add the casks tap, if you haven't already:



        brew tap homebrew/cask-versions



      3. Look for installable versions:



        brew search java



      4. Check the details on the version that will be installed:



        brew cask info java


        Note: prior to October 3, 2018 that the current release is the Oracle commercial version of Java 11, and after October 3 this is now OpenJDK 11.




      5. Install a specific version of the JDK such as java8, java10 or java for the current:



        brew cask install java


        The AdoptOpenJDK version is available as well now from Homebrew Cask:



        brew cask install adoptopenjdk



      And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.



      Other installation options:



      Some other flavours of openJDK are:



      Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.




      Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.




      Amazon Correto OpenJDK builds have an easy to use installation package for version 8 (other versions are coming), and installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.




      Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Amazon Linux 2, Windows, and macOS.






      Where is my JDK?!?!



      To find locations of previously installed Java JDK's installed at the default system locations, use:



      /usr/libexec/java_home -V



      Matching Java Virtual Machines (8):
      11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
      9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
      1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home




      You can also report just the location of a specific Java version using -v



      /usr/libexec/java_home -v 11



      /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home




      Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba -- and you need to know where to find them.



      If you need to find JDK's instaled by other tools, check these locations:




      • SDKMAN installs to ~/.sdkman/candidates/java/

      • Jabba installs to ~/.jabba/jdk




      Switching versions manually



      The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.



      For example, if you installed or untar'd JDK 11 to /Library/Java/JavaVirtualMachines/jdk-11.jdk if it is the highest version number it should already be the default, if not you could simply set:



      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And now whatever Java executable is in the path will see this and use the correct JDK.



      Using the /usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your .profile or .bash_profile to change JAVA_HOME for you:



      export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
      export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
      export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
      export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

      alias java8='export JAVA_HOME=$JAVA_8_HOME'
      alias java9='export JAVA_HOME=$JAVA_9_HOME'
      alias java10='export JAVA_HOME=$JAVA_10_HOME'
      alias java11='export JAVA_HOME=$JAVA_11_HOME'

      # default to Java 11
      java11


      Then to change versions, just use the alias.



      java8
      java -version



      java version "1.8.0_144"




      Of course setting JAVA_HOME manually works too!





      Switching versions with JEnv



      JEnv expects the Java JDK's to already exist on the machine and can be in any location. Typically you will find installed Java JDK's in /Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.




      1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.



      2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):



        jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home



      3. Set your global version using this command:



        jenv global 11



      You can also add other existing versions using jenv add in a similar manner, and list those that are available. For example Java 8:



      jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 
      jenv versions


      See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.



      To help manage JAVA_HOME while using JEnv you can add the export plugin to do this for you.



      $ jenv enable-plugin export
      You may restart your session to activate jenv export plugin echo export plugin activated


      The export plugin may not adjust JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.



      You can also use jenv exec <command> <parms...> to run single commands with JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.





      Installing and Switching versions with SDKMAN



      SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.




      1. Install SDKMAN from https://sdkman.io/install



      2. Install Java 11 using SDKMAN:



        sdk install java 11.0.0-open 



      3. Make 11 the default version:



        sdk default java 11.0.0-open


        Or switch to 11 for the session:



        sdk use java 11.0.0-open



      You can list available versions for installation using the list command:



      sdk list java


      And install additional versions, such as JDK 8:



      sdk install java 8.0.181-oracle


      SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:



      sdk install java my-local-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And use it freely:



      sdk use java my-local-11


      More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.



      SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.





      Installing and Switching versions with Jabba



      Jabba also handles both the install and the switching. Jabba also places the installed JDK's into its own directory tree, which is typically ~/.jabba/jdk.




      1. Install Jabba by following the instructions on the home page.



      2. List available JDK's



        jabba ls-remote



      3. Install Java JDK 11



        jabba install openjdk@1.11.0



      4. Use it:



        jabba use openjdk@1.11.0



      You can also alias version names, link to existing JDK's already installed, and find a mix of interesting JDK's such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.



      Jabba will automatically manage your PATH and JAVA_HOME for you as you change versions.






      share|improve this answer















      note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, and even early access Java 12!



      You have a few options of how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting JAVA_HOME. All of these are described below.







      Installation



      First, install Java using whatever method you prefer including Homebrew, SDKMAN or a manual install of the tar.gz file. The advantages of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.



      Install with SDKMAN



      This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.



      <see below "Installing and Switching versions with SDKMAN">



      Install using Jabba



      This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.



      <see below "Installing and Switching versions with Jabba">



      Install manually from OpenJDK download page:




      1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/ (or early access JDK 12 from http://jdk.java.net/12/)


      2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.



      Install with Homebrew



      The version of Java available in Homebrew Cask previos to October 3, 2018 was indeed the Oracle JVM. Now however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.





      1. install Homebrew if you haven't already. Make sure it is updated:



        brew update



      2. Add the casks tap, if you haven't already:



        brew tap homebrew/cask-versions



      3. Look for installable versions:



        brew search java



      4. Check the details on the version that will be installed:



        brew cask info java


        Note: prior to October 3, 2018 that the current release is the Oracle commercial version of Java 11, and after October 3 this is now OpenJDK 11.




      5. Install a specific version of the JDK such as java8, java10 or java for the current:



        brew cask install java


        The AdoptOpenJDK version is available as well now from Homebrew Cask:



        brew cask install adoptopenjdk



      And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.



      Other installation options:



      Some other flavours of openJDK are:



      Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.




      Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.




      Amazon Correto OpenJDK builds have an easy to use installation package for version 8 (other versions are coming), and installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.




      Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Amazon Linux 2, Windows, and macOS.






      Where is my JDK?!?!



      To find locations of previously installed Java JDK's installed at the default system locations, use:



      /usr/libexec/java_home -V



      Matching Java Virtual Machines (8):
      11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
      9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
      1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home




      You can also report just the location of a specific Java version using -v



      /usr/libexec/java_home -v 11



      /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home




      Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba -- and you need to know where to find them.



      If you need to find JDK's instaled by other tools, check these locations:




      • SDKMAN installs to ~/.sdkman/candidates/java/

      • Jabba installs to ~/.jabba/jdk




      Switching versions manually



      The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.



      For example, if you installed or untar'd JDK 11 to /Library/Java/JavaVirtualMachines/jdk-11.jdk if it is the highest version number it should already be the default, if not you could simply set:



      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And now whatever Java executable is in the path will see this and use the correct JDK.



      Using the /usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your .profile or .bash_profile to change JAVA_HOME for you:



      export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
      export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
      export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
      export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

      alias java8='export JAVA_HOME=$JAVA_8_HOME'
      alias java9='export JAVA_HOME=$JAVA_9_HOME'
      alias java10='export JAVA_HOME=$JAVA_10_HOME'
      alias java11='export JAVA_HOME=$JAVA_11_HOME'

      # default to Java 11
      java11


      Then to change versions, just use the alias.



      java8
      java -version



      java version "1.8.0_144"




      Of course setting JAVA_HOME manually works too!





      Switching versions with JEnv



      JEnv expects the Java JDK's to already exist on the machine and can be in any location. Typically you will find installed Java JDK's in /Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.




      1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.



      2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):



        jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home



      3. Set your global version using this command:



        jenv global 11



      You can also add other existing versions using jenv add in a similar manner, and list those that are available. For example Java 8:



      jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 
      jenv versions


      See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.



      To help manage JAVA_HOME while using JEnv you can add the export plugin to do this for you.



      $ jenv enable-plugin export
      You may restart your session to activate jenv export plugin echo export plugin activated


      The export plugin may not adjust JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.



      You can also use jenv exec <command> <parms...> to run single commands with JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.





      Installing and Switching versions with SDKMAN



      SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.




      1. Install SDKMAN from https://sdkman.io/install



      2. Install Java 11 using SDKMAN:



        sdk install java 11.0.0-open 



      3. Make 11 the default version:



        sdk default java 11.0.0-open


        Or switch to 11 for the session:



        sdk use java 11.0.0-open



      You can list available versions for installation using the list command:



      sdk list java


      And install additional versions, such as JDK 8:



      sdk install java 8.0.181-oracle


      SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:



      sdk install java my-local-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home


      And use it freely:



      sdk use java my-local-11


      More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.



      SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.





      Installing and Switching versions with Jabba



      Jabba also handles both the install and the switching. Jabba also places the installed JDK's into its own directory tree, which is typically ~/.jabba/jdk.




      1. Install Jabba by following the instructions on the home page.



      2. List available JDK's



        jabba ls-remote



      3. Install Java JDK 11



        jabba install openjdk@1.11.0



      4. Use it:



        jabba use openjdk@1.11.0



      You can also alias version names, link to existing JDK's already installed, and find a mix of interesting JDK's such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.



      Jabba will automatically manage your PATH and JAVA_HOME for you as you change versions.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 22 '18 at 0:34

























      answered Sep 26 '18 at 18:32









      Jayson MinardJayson Minard

      39.5k17109173




      39.5k17109173








      • 2





        For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

        – Peter Hull
        Sep 27 '18 at 8:20











      • @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

        – Jayson Minard
        Sep 29 '18 at 6:39











      • quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

        – Peter Hull
        Sep 30 '18 at 17:52






      • 2





        Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

        – Xingang Huang
        Oct 4 '18 at 17:30






      • 4





        Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

        – Kevin Hooke
        Oct 20 '18 at 19:44














      • 2





        For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

        – Peter Hull
        Sep 27 '18 at 8:20











      • @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

        – Jayson Minard
        Sep 29 '18 at 6:39











      • quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

        – Peter Hull
        Sep 30 '18 at 17:52






      • 2





        Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

        – Xingang Huang
        Oct 4 '18 at 17:30






      • 4





        Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

        – Kevin Hooke
        Oct 20 '18 at 19:44








      2




      2





      For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

      – Peter Hull
      Sep 27 '18 at 8:20





      For manual install I think you need to set JAVA_HOME in order to select a specific version, something like export JAVA_HOME=$(/usr/libexec/java_home -v <version>) in your .bash_profile

      – Peter Hull
      Sep 27 '18 at 8:20













      @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

      – Jayson Minard
      Sep 29 '18 at 6:39





      @PeterHull some of the Java version managers do not like having JAVA_HOME set manually and they manage it themselves. JEnv for sure will not adjust it if previously set, not sure about Jabba and SDKMAN but they normally will manage that setting. Only a manual install with NO version switching should ever set this value directly.

      – Jayson Minard
      Sep 29 '18 at 6:39













      quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

      – Peter Hull
      Sep 30 '18 at 17:52





      quite agree, it would be good if you could note that in your answer as the original question asked for version switching and setting JAVA_HOME is the (only?) way to do it for the manual install method.

      – Peter Hull
      Sep 30 '18 at 17:52




      2




      2





      Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

      – Xingang Huang
      Oct 4 '18 at 17:30





      Java in brew cask changed to OpenJDK 11 yesterday, Oct 3, 2018.

      – Xingang Huang
      Oct 4 '18 at 17:30




      4




      4





      Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

      – Kevin Hooke
      Oct 20 '18 at 19:44





      Possibly the most extensive and useful answer to running different JDKs on MacOS covering many different approaches, some I'd never come across before.

      – Kevin Hooke
      Oct 20 '18 at 19:44













      2














      Manually switching system-default version without 3rd party tools:



      As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.



      Armed with this knowledge you can:




      • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).

      • control which version to use for specific tasks by setting $JAVA_HOME


      I've just verified this is still true with OpenJDK & Mojave.



      On a brand new system, there is no Java version installed:



      $ java -version
      No Java runtime present, requesting install.


      Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
      install OpenJDK11:



      $ cd /Library/Java/JavaVirtualMachines/
      $ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz


      System java is now 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      Install OpenJDK12 (early access at the moment):



      $ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 


      System java is now 12:



      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]


      Now let's "hide" OpenJDK 12 from system java wrapper:



      $ cd jdk-12.jdk/Contents/
      $ sudo mv Info.plist Info.plist.disabled


      System java is back to 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      And you can still use version 12 punctually by manually setting JAVA_HOME:



      $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]





      share|improve this answer
























      • and it should also be true now with AdoptOpenJDK builds

        – Hugues M.
        Oct 30 '18 at 21:29
















      2














      Manually switching system-default version without 3rd party tools:



      As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.



      Armed with this knowledge you can:




      • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).

      • control which version to use for specific tasks by setting $JAVA_HOME


      I've just verified this is still true with OpenJDK & Mojave.



      On a brand new system, there is no Java version installed:



      $ java -version
      No Java runtime present, requesting install.


      Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
      install OpenJDK11:



      $ cd /Library/Java/JavaVirtualMachines/
      $ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz


      System java is now 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      Install OpenJDK12 (early access at the moment):



      $ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 


      System java is now 12:



      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]


      Now let's "hide" OpenJDK 12 from system java wrapper:



      $ cd jdk-12.jdk/Contents/
      $ sudo mv Info.plist Info.plist.disabled


      System java is back to 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      And you can still use version 12 punctually by manually setting JAVA_HOME:



      $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]





      share|improve this answer
























      • and it should also be true now with AdoptOpenJDK builds

        – Hugues M.
        Oct 30 '18 at 21:29














      2












      2








      2







      Manually switching system-default version without 3rd party tools:



      As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.



      Armed with this knowledge you can:




      • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).

      • control which version to use for specific tasks by setting $JAVA_HOME


      I've just verified this is still true with OpenJDK & Mojave.



      On a brand new system, there is no Java version installed:



      $ java -version
      No Java runtime present, requesting install.


      Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
      install OpenJDK11:



      $ cd /Library/Java/JavaVirtualMachines/
      $ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz


      System java is now 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      Install OpenJDK12 (early access at the moment):



      $ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 


      System java is now 12:



      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]


      Now let's "hide" OpenJDK 12 from system java wrapper:



      $ cd jdk-12.jdk/Contents/
      $ sudo mv Info.plist Info.plist.disabled


      System java is back to 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      And you can still use version 12 punctually by manually setting JAVA_HOME:



      $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]





      share|improve this answer













      Manually switching system-default version without 3rd party tools:



      As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.



      Armed with this knowledge you can:




      • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).

      • control which version to use for specific tasks by setting $JAVA_HOME


      I've just verified this is still true with OpenJDK & Mojave.



      On a brand new system, there is no Java version installed:



      $ java -version
      No Java runtime present, requesting install.


      Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
      install OpenJDK11:



      $ cd /Library/Java/JavaVirtualMachines/
      $ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz


      System java is now 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      Install OpenJDK12 (early access at the moment):



      $ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 


      System java is now 12:



      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]


      Now let's "hide" OpenJDK 12 from system java wrapper:



      $ cd jdk-12.jdk/Contents/
      $ sudo mv Info.plist Info.plist.disabled


      System java is back to 11:



      $ java -version
      openjdk version "11.0.1" 2018-10-16
      [...]


      And you can still use version 12 punctually by manually setting JAVA_HOME:



      $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
      $ java -version
      openjdk version "12-ea" 2019-03-19
      [...]






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 28 '18 at 11:49









      Hugues M.Hugues M.

      12.5k51743




      12.5k51743













      • and it should also be true now with AdoptOpenJDK builds

        – Hugues M.
        Oct 30 '18 at 21:29



















      • and it should also be true now with AdoptOpenJDK builds

        – Hugues M.
        Oct 30 '18 at 21:29

















      and it should also be true now with AdoptOpenJDK builds

      – Hugues M.
      Oct 30 '18 at 21:29





      and it should also be true now with AdoptOpenJDK builds

      – Hugues M.
      Oct 30 '18 at 21:29











      1














      This is how I did it.



      Step 1: Install Java 11



      You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html



      Step 2: After installation of Java 11. Confirm installation of all versions. Type the following command in your terminal.



      /usr/libexec/java_home -V


      Step 3: Edit .bash_profile



      sudo nano ~/.bash_profile


      Step 4: Add 11.0.1 as default. (Add below line to bash_profile file).



      export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)


      to switch to any version



      export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)


      Now Press CTRL+X to exit the bash. Press 'Y' to save changes.



      Step 5: Reload bash_profile



      source ~/.bash_profile


      Step 6: Confirm current version of Java



      java -version





      share|improve this answer






























        1














        This is how I did it.



        Step 1: Install Java 11



        You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html



        Step 2: After installation of Java 11. Confirm installation of all versions. Type the following command in your terminal.



        /usr/libexec/java_home -V


        Step 3: Edit .bash_profile



        sudo nano ~/.bash_profile


        Step 4: Add 11.0.1 as default. (Add below line to bash_profile file).



        export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)


        to switch to any version



        export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)


        Now Press CTRL+X to exit the bash. Press 'Y' to save changes.



        Step 5: Reload bash_profile



        source ~/.bash_profile


        Step 6: Confirm current version of Java



        java -version





        share|improve this answer




























          1












          1








          1







          This is how I did it.



          Step 1: Install Java 11



          You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html



          Step 2: After installation of Java 11. Confirm installation of all versions. Type the following command in your terminal.



          /usr/libexec/java_home -V


          Step 3: Edit .bash_profile



          sudo nano ~/.bash_profile


          Step 4: Add 11.0.1 as default. (Add below line to bash_profile file).



          export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)


          to switch to any version



          export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)


          Now Press CTRL+X to exit the bash. Press 'Y' to save changes.



          Step 5: Reload bash_profile



          source ~/.bash_profile


          Step 6: Confirm current version of Java



          java -version





          share|improve this answer















          This is how I did it.



          Step 1: Install Java 11



          You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html



          Step 2: After installation of Java 11. Confirm installation of all versions. Type the following command in your terminal.



          /usr/libexec/java_home -V


          Step 3: Edit .bash_profile



          sudo nano ~/.bash_profile


          Step 4: Add 11.0.1 as default. (Add below line to bash_profile file).



          export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)


          to switch to any version



          export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)


          Now Press CTRL+X to exit the bash. Press 'Y' to save changes.



          Step 5: Reload bash_profile



          source ~/.bash_profile


          Step 6: Confirm current version of Java



          java -version






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 6:08

























          answered Nov 13 '18 at 3:11









          VK321VK321

          2,18121525




          2,18121525























              0














              Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/



              First install SDKMAN: https://sdkman.io/install and then...




              1. Install Oracle JDK 8 with: sdk install java 8.0.181-oracle

              2. Install OpenJDK 11 with: sdk install java 11.0.0-open


              To switch:




              • Switch to JDK 8 with sdk use java 8.0.181-oracle

              • Switch to JDK 11 with sdk use java 11.0.0-open


              To set a default:




              • Default to JDK 8 with sdk default java 8.0.181-oracle

              • Default to JDK 11 with sdk default java 11.0.0-open






              share|improve this answer






























                0














                Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/



                First install SDKMAN: https://sdkman.io/install and then...




                1. Install Oracle JDK 8 with: sdk install java 8.0.181-oracle

                2. Install OpenJDK 11 with: sdk install java 11.0.0-open


                To switch:




                • Switch to JDK 8 with sdk use java 8.0.181-oracle

                • Switch to JDK 11 with sdk use java 11.0.0-open


                To set a default:




                • Default to JDK 8 with sdk default java 8.0.181-oracle

                • Default to JDK 11 with sdk default java 11.0.0-open






                share|improve this answer




























                  0












                  0








                  0







                  Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/



                  First install SDKMAN: https://sdkman.io/install and then...




                  1. Install Oracle JDK 8 with: sdk install java 8.0.181-oracle

                  2. Install OpenJDK 11 with: sdk install java 11.0.0-open


                  To switch:




                  • Switch to JDK 8 with sdk use java 8.0.181-oracle

                  • Switch to JDK 11 with sdk use java 11.0.0-open


                  To set a default:




                  • Default to JDK 8 with sdk default java 8.0.181-oracle

                  • Default to JDK 11 with sdk default java 11.0.0-open






                  share|improve this answer















                  Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/



                  First install SDKMAN: https://sdkman.io/install and then...




                  1. Install Oracle JDK 8 with: sdk install java 8.0.181-oracle

                  2. Install OpenJDK 11 with: sdk install java 11.0.0-open


                  To switch:




                  • Switch to JDK 8 with sdk use java 8.0.181-oracle

                  • Switch to JDK 11 with sdk use java 11.0.0-open


                  To set a default:




                  • Default to JDK 8 with sdk default java 8.0.181-oracle

                  • Default to JDK 11 with sdk default java 11.0.0-open







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 26 '18 at 19:36









                  Jayson Minard

                  39.5k17109173




                  39.5k17109173










                  answered Sep 26 '18 at 19:17









                  Wim DeblauweWim Deblauwe

                  10.3k969135




                  10.3k969135























                      -1














                      IMHO, There is no need to install all the additional applications/packages.



                      Check available versions using the command:



                      > /usr/libexec/java_home -V
                      Matching Java Virtual Machines (8):
                      11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
                      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
                      9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
                      1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
                      1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
                      1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
                      1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
                      1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


                      Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:



                      > /usr/libexec/java_home -v 1.8.0_181
                      /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home


                      To pick Oracle's Java SE 8 you would invoke the command:



                      > /usr/libexec/java_home -v 1.8.0_151
                      /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home


                      As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151






                      share|improve this answer


























                      • Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                        – Jayson Minard
                        Oct 7 '18 at 11:37











                      • This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                        – Jayson Minard
                        Oct 7 '18 at 12:01
















                      -1














                      IMHO, There is no need to install all the additional applications/packages.



                      Check available versions using the command:



                      > /usr/libexec/java_home -V
                      Matching Java Virtual Machines (8):
                      11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
                      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
                      9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
                      1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
                      1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
                      1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
                      1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
                      1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


                      Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:



                      > /usr/libexec/java_home -v 1.8.0_181
                      /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home


                      To pick Oracle's Java SE 8 you would invoke the command:



                      > /usr/libexec/java_home -v 1.8.0_151
                      /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home


                      As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151






                      share|improve this answer


























                      • Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                        – Jayson Minard
                        Oct 7 '18 at 11:37











                      • This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                        – Jayson Minard
                        Oct 7 '18 at 12:01














                      -1












                      -1








                      -1







                      IMHO, There is no need to install all the additional applications/packages.



                      Check available versions using the command:



                      > /usr/libexec/java_home -V
                      Matching Java Virtual Machines (8):
                      11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
                      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
                      9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
                      1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
                      1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
                      1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
                      1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
                      1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


                      Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:



                      > /usr/libexec/java_home -v 1.8.0_181
                      /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home


                      To pick Oracle's Java SE 8 you would invoke the command:



                      > /usr/libexec/java_home -v 1.8.0_151
                      /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home


                      As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151






                      share|improve this answer















                      IMHO, There is no need to install all the additional applications/packages.



                      Check available versions using the command:



                      > /usr/libexec/java_home -V
                      Matching Java Virtual Machines (8):
                      11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
                      10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
                      9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
                      1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
                      1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
                      1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
                      1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
                      1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


                      Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:



                      > /usr/libexec/java_home -v 1.8.0_181
                      /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home


                      To pick Oracle's Java SE 8 you would invoke the command:



                      > /usr/libexec/java_home -v 1.8.0_151
                      /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home


                      As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 5 '18 at 11:53

























                      answered Oct 5 '18 at 10:18









                      Ranganath SamudralaRanganath Samudrala

                      415




                      415













                      • Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                        – Jayson Minard
                        Oct 7 '18 at 11:37











                      • This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                        – Jayson Minard
                        Oct 7 '18 at 12:01



















                      • Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                        – Jayson Minard
                        Oct 7 '18 at 11:37











                      • This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                        – Jayson Minard
                        Oct 7 '18 at 12:01

















                      Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                      – Jayson Minard
                      Oct 7 '18 at 11:37





                      Shouldn't the first example have a version string 1.8.0_181-zulu-8.31.0.1 ? or is it partial matching?

                      – Jayson Minard
                      Oct 7 '18 at 11:37













                      This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                      – Jayson Minard
                      Oct 7 '18 at 12:01





                      This doesn't actually change anything to use the version of Java, it just reports the location of the JAVA_HOME. Manual switching using these commands is covered in my other answer.

                      – Jayson Minard
                      Oct 7 '18 at 12:01


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52524112%2fhow-do-i-install-java-on-mac-osx-allowing-version-switching%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma