How To Install / Uninstall Multiple Java Versions In Mac OS By Home Brew Or Manually

This article will tell you how to install/uninstall multiple java versions on mac os both use homebrew or manually. It also shows you how to set JAVA_HOME & PATH system environment variable to make your installed JDK the default JDK. We recommend you use homebrew because it provides a lot of easy-to-use tools.

1. Use HomeBrew To Install & Uninstall Java On Mac OS X.

1.1 Use HomeBrew Cask Command To Install Java JDK.

  1. Install HomeBrew on your mac os, please read the article How To Install HomeBrew On Mac OS.
  2. Run the below command to install the homebrew cask extension, cask extension tool can help you to install and manage software packages easily.
    $ brew tap homebrew/cask-versions
    Updating Homebrew...
    ==> Auto-updated Homebrew!
    Updated 1 tap (homebrew/core).
    ==> Updated Formulae
    node-build
    
    ==> Tapping homebrew/cask-versions
    Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions'...
    remote: Enumerating objects: 198, done.
    remote: Counting objects: 100% (198/198), done.
    remote: Compressing objects: 100% (194/194), done.
    remote: Total 198 (delta 9), reused 29 (delta 1), pack-reused 0
    Receiving objects: 100% (198/198), 84.76 KiB | 221.00 KiB/s, done.
    Resolving deltas: 100% (9/9), done.
    Tapped 169 casks (216 files, 324.9KB).
  3. Run brew search java or brew search jdk command to find your desired java or JDK version. Below is the search result for the command brew search java.
    $ brew search java
    ==> Formulae
    app-engine-java                                     google-java-format                                  javarepl                                            jslint4java                                         libreadline-java
    
    ==> Casks
    charles-applejava      eclipse-java           eclipse-javascript     java ✔                 java-beta              java11                 java6                  netbeans-java-ee       netbeans-java-se       oracle-jdk-javadoc     yourkit-java-profiler
    charles-applejava      eclipse-java           eclipse-javascript     java ✔                 java-beta              java11                 java6                  netbeans-java-ee       netbeans-java-se       oracle-jdk-javadoc     yourkit-java-profiler

    Below is the search result for the command brew search jdk.

    $ brew search jdk
    ==> Casks
    adoptopenjdk              adoptopenjdk              adoptopenjdk8             adoptopenjdk8             oracle-jdk                oracle-jdk                oracle-jdk-javadoc        oracle-jdk-javadoc        sapmachine-jdk            sapmachine-jdk
  4. Now install the JDK version that you need like below. During the installation process, you may encounter some error messages like Error: Cask ‘java8’ is unavailable: No Cask with this name exists or Error: Cask adoptopenjdk8 exists in multiple taps.

    $ brew cask install java8
    Error: Cask 'java8' is unavailable: No Cask with this name exists.
    
    $ brew cask install adoptopenjdk8
    Error: Cask adoptopenjdk8 exists in multiple taps:
      homebrew/cask-versions/adoptopenjdk8
      caskroom/versions/adoptopenjdk8
    
  5. Below is the correct command to install JDK 8 use the homebrew cask command ( $ brew cask install homebrew/cask-versions/adoptopenjdk8 ). You can also run $ brew cask install java to install the newest JDK version. In this example, I had installed the newest JDK version 12.0.1.
    $ brew cask install homebrew/cask-versions/adoptopenjdk8
    ==> Satisfying dependencies
    ==> Downloading https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u212-b03/OpenJDK8U-jdk_x64_mac_hotspot_8u212b03.pkg
    ==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/140418865/07e4b900-61d1-11e9-96f2-868c40733c49?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190603%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=2019
    ######################################################################## 100.0%
    ==> Verifying SHA-256 checksum for Cask 'adoptopenjdk8'.
    ==> Installing Cask adoptopenjdk8
    ==> Running installer for adoptopenjdk8; your password may be necessary.
    ==> Package installers may write to any location; options such as --appdir are ignored.
    Password:
    installer: Package name is AdoptOpenJDK
    installer: Installing at base path /
    installer: The install was successful.
    ?  adoptopenjdk8 was successfully installed!
    
  6. After install you can first run the command /usr/libexec/java_home to get the java home directory, then go to the JDK home directory to see all the installed JDK.
    # Get the java home directory info.
    
    $ /usr/libexec/java_home 
    
    /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    
    
    # List all the installed jdk in java home directory.
    
    $ ls -l /Library/Java/JavaVirtualMachines/
    
    total 0
    drwxr-xr-x  3 root      wheel  96 Jun  3 11:19 adoptopenjdk-8.jdk
    drwxr-xr-x  3 root      wheel  96 May  3 22:31 jdk-12.0.1.jdk
    drwxr-xr-x@ 3 songzhao  staff  96 Apr  2 16:23 openjdk-12.0.1.jdk
  7. From the above output, we can see that there are three java JDK that has been installed, two open JDK, and one standard JDK. Run each JDK’s java executable command, you can get the below output.
    $ ./openjdk-12.0.1.jdk/Contents/Home/bin/java -version
    openjdk version "12.0.1" 2019-04-16
    OpenJDK Runtime Environment (build 12.0.1+12)
    OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
    
    
    $ ./adoptopenjdk-8.jdk/Contents/Home/bin/java -version
    openjdk version "1.8.0_212"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)
    
    
    $ ./jdk-12.0.1.jdk/Contents/Home/bin/java -version
    java version "12.0.1" 2019-04-16
    Java(TM) SE Runtime Environment (build 12.0.1+12)
    Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

1.2 Use HomeBrew Cask Command To UnInstall JDK.

  1. Run homebrew cask command $ brew cask uninstall java to uninstall open JDK 12.0.1. If you want to uninstall open JDK 8 then run $ brew cask uninstall caskroom/versions/adoptopenjdk8.
    The below command uninstalls Open JDK 12.0.1.

    $ brew cask uninstall java
    ==> Uninstalling Cask java
    ==> Backing Generic Artifact 'openjdk-12.0.1.jdk' up to '/usr/local/Caskroom/java/12.0.1,69cfe15208a647278a19ef0990eea691/jdk-12.0.1.jdk'.
    Password:
    ==> Removing Generic Artifact '/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk'.
    ==> Removing directories if empty:
    /Library/Java/JavaVirtualMachines
    ==> Purging files for version 12.0.1,69cfe15208a647278a19ef0990eea691 of Cask java
    

    The below command uninstalls Open JDK 8.

    $ brew cask uninstall caskroom/versions/adoptopenjdk8
    ==> Uninstalling Cask adoptopenjdk8
    ==> Uninstalling packages:
    net.adoptopenjdk.8.jdk
    Password:
    ==> Purging files for version 8,212:b03 of Cask adoptopenjdk8

    During the above JDK uninstall process, you may encounter the below error, Error: Cask adoptopenjdk8 exists in multiple taps. This is because you should uninstall caskroom/versions/adoptopenjdk8 instead of adoptopenjdk8.

    $ brew cask uninstall adoptopenjdk8
    Error: Cask adoptopenjdk8 exists in multiple taps:
      homebrew/cask-versions/adoptopenjdk8
      caskroom/versions/adoptopenjdk8
    
  2. Now go to the JDK installation directory, you will find above two homebrew installed JDK directory has been removed.
    $ ls -l /Library/Java/JavaVirtualMachines/
    total 0
    drwxr-xr-x  3 root  wheel  96 May  3 22:31 jdk-12.0.1.jdk

2. Install & Uninstall JDK In Mac OS Manually.

2.1 Install JDK Manually In Mac OS X.

  1. Download related JDK from the Oracle JDK download page. You should have an oracle account to log in before the download start.
  2. Double click the downloaded dmg file to install JDK, just follow the wizard to install it.
  3. When the installation is complete, you can find the JDK install directory in the folder /Library/Java/JavaVirtualMachines/. From the below output, we can see there are three JDK that has been installed, one ( adoptopenjdk-8.jdk ) is installed with homebrew, and the other two ( jdk-12.0.1.jdk, jdk1.8.0_211.jdk ) are installed by download installation file from oracle.
    $ ls -l /Library/Java/JavaVirtualMachines/
    total 0
    drwxr-xr-x  3 root  wheel  96 Jun  3 14:02 adoptopenjdk-8.jdk
    drwxr-xr-x  3 root  wheel  96 May  3 22:31 jdk-12.0.1.jdk
    
    drwxr-xr-x  3 root  wheel  96 Jun  3 15:54 jdk1.8.0_211.jdk

2.2 Uninstall Java JDK Manually In Mac OS X.

Follow oracle’s documents, to manually uninstall installed JDK in mac os x, you just need to remove some files with root permission in the terminal like below.

  1. Open a terminal and run the below command to remove the java JDK installed directory.
    $ sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk
    
    $ ls -l /Library/Java/JavaVirtualMachines/
    total 0
    drwxr-xr-x  3 root  wheel  96 Jun  3 14:02 adoptopenjdk-8.jdk
    drwxr-xr-x  3 root  wheel  96 May  3 22:31 jdk-12.0.1.jdk
  2. Run the below command to remove the java control panel if exist.
    $ sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane
  3. Remove java applet plugins.
    $ sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/
  4. Remove java application support.
    $ sudo rm -rf ~/Library/Application\ Support/Java
  5. Remove the java updater list.
    $ sudo rm -rf /Library/LaunchAgents/com.oracle.java.Java-Updater.plist
    $ sudo rm -rf /Library/PrivilegedHelperTools/com.oracle.java.JavaUpdateHelper
  6. Remove the java helper tool.
    $ sudo rm -rf /Library/LaunchDaemons/com.oracle.java.Helper-Tool.plist
    
    $ sudo rm -rf /Library/Preferences/com.oracle.java.Helper-Tool.plist

3. Set JAVA_HOME & PATH System Environment Variable.

Now we have known how to install/uninstall multiple java JDK versions on mac os. But after that, you need to set JAVA_HOME & PATH environment variable value to set your installed JDK as the default JDK. So that you can use it easily.

  1. Run the below command in a terminal to go to the user’s home directory.
    $ cd ~
    $ pwd
    /Users/songzhao
  2. Run $ vim .bash_profile command to open .bash_profile file to edit.
    $vim .bash_profile
  3. Press esc then press i to enter insert mode and insert the below export command in .bash_profile file.
    # Set JAVA_HOME system environment variable value.
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    
    # Add java bin folder in PATH system environment variable value.
    
    export PATH=$PATH:$JAVA_HOME/bin
  4. Press esc then :wq! to save the above changes and quit the vim editor.
  5. Run $ source .bash_profile command to make the above changes take effect.
  6. Now run $ java -version, you can see the new JDK version is used by output info.
    $ java -version
    openjdk version "1.8.0_212"
    
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
    
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)

4. Question & Answer.

4.1 How to manage multiple JDK versions on Mac OS X use SDKMAN.

  1. My macOS is Catalina Version 10.15.7, and I run the command /usr/libexec/java_home -verbose to find all the installed JDK versions. I installed JDK 12.0.1 and OpenJDK 1.8.0.
    $ /usr/libexec/java_home -verbose
    Matching Java Virtual Machines (2):
    12.0.1, x86_64: "Java SE 12.0.1" /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    1.8.0_212, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    
    /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
  2. Now I want to install and manage more other JDK versions in it, how to manage the multiple JDK versions, thanks.
  3. You can use SDKMAN to manage multiple JDK versions. The SDKMAN supports all Unix-style OS. Open a terminal and run the command curl -s "https://get.sdkman.io" | bash to install it.
    $ curl -s "https://get.sdkman.io" | bash
    
     ......
                                                                     Now attempting installation...
    Looking for a previous installation of SDKMAN...
    Looking for unzip...
    Looking for zip...
    Looking for curl...
    Looking for sed...
    Installing SDKMAN scripts...
    Create distribution directories...
    Getting available candidates...
    Prime the config file...
    Download script archive...
    ######################################################################## 100.0%
    Extract script archive...
    Install scripts...
    Install contributed software...
    /Users/songzhao/.sdkman/tmp/stage/contrib/completion -> /Users/songzhao/.sdkman/contrib/completion
    Set version to 5.11.2+698 ...
    Attempt update of login bash profile on OSX...
    Added sdkman init snippet to /Users/songzhao/.bash_profile
    Attempt update of zsh profile...
    Updated existing /Users/songzhao/.zshrc
    
    
    
    All done!
    
    
    Please open a new terminal, or run the following in the existing one:
    
        source "/Users/songzhao/.sdkman/bin/sdkman-init.sh"
    
    Then issue the following command:
    
        sdk help
    
    Enjoy!!!
    
  4. Open a new terminal and run the command $ source "$HOME/.sdkman/bin/sdkman-init.sh" to initialize the SDKMAN.
  5. Run the command $ sdk version to verify the SDKMAN version and make sure it is installed successfully.
    $ sdk version
    ==== BROADCAST =================================================================
    * 2021-05-05: micronaut 2.5.1 available on SDKMAN!
    * 2021-05-05: kotlin 1.5.0 available on SDKMAN!
    * 2021-05-04: asciidoctorj 2.5.1 available on SDKMAN!
    ================================================================================
    
    SDKMAN 5.11.2+698
  6. Now you can run the command $ sdk list java to list all the available JDK versions you can install and manage with SDKMAN.
    $ sdk list java
    ================================================================================
    Available Java Versions
    ================================================================================
     Vendor        | Use | Version      | Dist    | Status     | Identifier
    --------------------------------------------------------------------------------
     AdoptOpenJDK  |     | 16.0.1.j9    | adpt    |            | 16.0.1.j9-adpt      
     ......
                   |     | 19.3.5.r11   | grl     |            | 19.3.5.r11-grl      
                   |     | 19.3.5.r8    | grl     |            | 19.3.5.r8-grl       
     Java.net      |     | 17.ea.20     | open    |            | 17.ea.20-open       
                   |     | 17.ea.19     | open    |            | 17.ea.19-open       
                   |     | 17.ea.6.lm   | open    |            | 17.ea.6.lm-open     
                   |     | 17.ea.2.pma  | open    |            | 17.ea.2.pma-open    
     ......
    ================================================================================
    Use the Identifier for installation:
    
        $ sdk install java 11.0.3.hs-adpt
    ================================================================================
    
  7. Run the command $ sdk install java jdk-identifier to install the JDK from the above list. You can find the JDK identifier from the last column of the list.
    $ sdk install java 16-open
    
    Downloading: java 16-open
    
    In progress...
    
    ############################################################################## 100.0%
    
    Repackaging Java 16-open...
    
    Done repackaging...
    Cleaning up residual files...
    
    Installing: java 16-open
    Done installing!
    
    
    Setting java 16-open as default.
    
  8. We can install two JDK versions, so we run the command $ sdk install java 15.0.2.7.1-amzn to install an amazon provided JDK.
    $ sdk install java 15.0.2.7.1-amzn
    
    Downloading: java 15.0.2.7.1-amzn
    
    In progress...
    
    ############################################################################## 100.0%
    
    Repackaging Java 15.0.2.7.1-amzn...
    
    Done repackaging...
    Cleaning up residual files...
    
    Installing: java 15.0.2.7.1-amzn
    Done installing!
    
    Do you want java 15.0.2.7.1-amzn to be set as default? (Y/n): n
  9. When the installation is complete, you can find the SDKMAN installed JDK in the directory

    ~/.sdkman/candidates/java/.

    $ ls -l ~/.sdkman/candidates/java/
    total 0
    drwxr-xr-x  10 songzhao  staff  320 Jan 21 06:44 15.0.2.7.1-amzn
    drwxr-xr-x   9 songzhao  staff  288 Feb  8 22:08 16-open
    lrwxr-xr-x   1 songzhao  staff    7 May  7 13:20 current -> 16-open
  10. You can switch to use one JDK with the command $sdk use java java-identifier.
    $ java -version
    openjdk version "16" 2021-03-16
    OpenJDK Runtime Environment (build 16+36-2231)
    OpenJDK 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)
    
    
    $ sdk use java 15.0.2.7.1-amzn
    
    Using java version 15.0.2.7.1-amzn in this shell.
    
    
    $ java -version
    openjdk version "15.0.2" 2021-01-19
    OpenJDK Runtime Environment Corretto-15.0.2.7.1 (build 15.0.2+7)
    OpenJDK 64-Bit Server VM Corretto-15.0.2.7.1 (build 15.0.2+7, mixed mode, sharing)
  11. You can also use SDKMAN to manage your existing JDK.
  12. First, get your existing JDK installed path with the command $ /usr/libexec/java_home -verbose.
    $ /usr/libexec/java_home -verbose
    Matching Java Virtual Machines (2):
        12.0.1, x86_64:	"Java SE 12.0.1"	/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
        1.8.0_212, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    
    /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
  13. What you need to do is just give your existing JDK a label in SDKMAN with the command $ sdk install java local-jdk-tag local-jdk-install-path.
    $ sdk install java my-local-jdk-8 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    Linking java my-local-jdk-8 to /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    Done installing!
    
  14. Now you can run the command $ sdk use java local-jdk-tag to use your local JDK version.
    $ sdk use java my-local-jdk-8
    
    Using java version my-local-jdk-8 in this shell.
    $ 
    $ java -version
    openjdk version "1.8.0_212"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)
  15. Now you can run the command env to find that SDKMAN has manipulated the JAVA_HOME system environment for you automatically.
    $ env
    
    JAVA_HOME=/Users/songzhao/.sdkman/candidates/java/my-local-jdk-8
  16. You can also find the SDKMAN_DIR and SDKMAN_CANDIDATES_DIR in the system environment variables.
    SDKMAN_DIR=/Users/songzhao/.sdkman
    CONDA_DEFAULT_ENV=base
    SDKMAN_CANDIDATES_DIR=/Users/songzhao/.sdkman/candidates

4.2 How to remove multiple JDK versions installed on the same Mac OS.

  1. There are 2 JDKs that have been installed on my macOS, and I want to remove all of them. The macOS version is High Sierra.
  2. First, I open a terminal and go to the directory /Library/Java/JavaVirtualMachines with the cd command.
  3. And then when I run the command rm – rf jdk1.7.8_09.jdk in the terminal, it throws an error message that said my user account permission is not enough.
  4. My account has administrator permission, and I also change to root user to run the command, and it is not successful also.
  5. What permission should I have to run the command rm – rf jdk1.7.8_09.jdk successfully to remove all the installed JDKs, thanks.
  6. You can follow the below steps to fix your issue.
  7. Open a terminal and go to the directory /Library/Java/JavaVirtualMachines.
  8. Then remove the folder that folder name format like jdkmajor.minor.macro[_update].jdk.
  9. It is a wrong action to remove Java tools in the /usr/bin directory.
  10. Because the /usr/bin directory is managed by macOS, and the macOS will reset all your changes in the directory /usr/bin when the macOS is upgraded next time.

References

  1. How To Set JAVA_HOME, MAVEN_HOME Environment Variable In MacOS

5 thoughts on “How To Install / Uninstall Multiple Java Versions In Mac OS By Home Brew Or Manually”

  1. I had installd JDK8 on my macOS, and it can work correctly. But my another java program need JDK7 to run. So I have too install both JDK8 and JDK7 on my macOS at the same time. But when I install the JDK7 by click the downloaded dmg file, it prompt a warning message which said there has a JDK installed which has new version than JDK7, and then the JDK7 installer quits. This is my issue and this article fix my issue. A very good article.

  2. Hi, I have set the environment variable path and ran the source .bash_profile command and able to see the changes. But when I open the terminal newly and hit echo $JAVA_HOMe I couldn’t see the path of JAVA_HOMe.Could you please help me with it.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.