This article will tell you how to install / uninstall multiple java versions on mac os both use home brew or manually. It also show you how to set JAVA_HOME & PATH system environment variable to make your installed jdk as the default jdk. We recommend you to use home brew, because it provide a lot of easy to use tools.
1. Use Home Brew To Install & Uninstall Java On Mac OS X.
1.1 Use Brew Cask Command To Install Java JDK.
- Install HomeBrew on your mac os, please read article How To Install Homebrew On Mac OS.
- Run below command to install home brew cask extension, cask extension tool can help you to install and manage software package 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).
- Run
brew search java
orbrew search jdk
command to find your desired java or jdk version.
Below is search result forbrew 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 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
- Now install the jdk version that you need like below. During the installation process, you may encounter some error message 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
- Below is the correct command to install jdk 8 use home brew 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!
- 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
- From 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 Brew Cask Command To UnInstall JDK.
- Run home brew 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 above jdk uninstall process, you may encounter 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
- Now go to jdk installation directory, you will find above two home brew 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.
- Download related jdk from oracle jdk download page. You should have an oracle account to login before download start.
- Double click the downloaded dmg file to install jdk, just follow the wizard to install it.
- When the installation complete, you can find the jdk install directory in folder /Library/Java/JavaVirtualMachines/. From below output, we can see there are three jdk has been installed, one ( adoptopenjdk-8.jdk ) is installed with home brew, 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 terminal like below.
- Open a terminal and run below command to remove 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
- Run below command to remove java control panel if exist.
$ sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane
- Remove java applet plugins.
$ sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/
- Remove java application support.
$ sudo rm -rf ~/Library/Application\ Support/Java
- Remove java updater list.
$ sudo rm -rf /Library/LaunchAgents/com.oracle.java.Java-Updater.plist $ sudo rm -rf /Library/PrivilegedHelperTools/com.oracle.java.JavaUpdateHelper
- Remove 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.
- Run below command in a terminal to go to user home directory.
$ cd ~ $ pwd /Users/songzhao
- Run
$ vim .bash_profile
command to open .bash_profile file to edit.$vim .bash_profile
- Press esc then press i to enter insert mode, and insert 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
- Press esc then :wq! to save above changes and quit vim editor.
- Run
$ source .bash_profile
command to make above changes take effect. - 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
Worked ! Thank you
THANKS!!!
Hi I have set the environment variable path and ran source .bash_profile command and able to see the changes .But when i open the terminal newly and hit echo $JAVA_HOMe i couldnt see the path of JAVA_HOMe.Could you please help me on it.