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 as the default JDK. We recommend you to 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 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 brew search java command.

    $ 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 brew search jdk command.

    $ 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 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 /usr/libexec/java_home command 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
    [email protected] 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 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.
    Below command uninstall 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
    

    Below command uninstall 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 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 oracle JDK download page. You should have an oracle account to log in before the download start.
    jdk-download-page
  2. Double click the downloaded dmg file to install JDK, just follow the wizard to install it.
  3. When the installation complete, you can find the JDK install directory in 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, 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 default JDK. So that you can use it easily.

  1. Run the below command in a terminal to go to the user 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)

References

  1. How To Set JAVA_HOME, MAVEN_HOME Environment Variable In MacOS
0 0 vote
Article Rating
Subscribe
Notify of
guest

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

5 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Daniel Griffin
Daniel Griffin

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.

Rasika
Rasika

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.

Gregory
Gregory

THANKS!!!

Anonymous Developer
Anonymous Developer

Worked ! Thank you

5
0
Would love your thoughts, please comment.x
()
x