illustrate
Tools for use: brew cask
brew cask is a tool that manages applications under Mac using the command line. It provides automatic installation and uninstall functions and can automatically download and install the latest version from the official website. It is an enhanced tool based on homebrew.
# How to not install brew cask. Please execute $ brew tap caskroom/versions$ brew cask install java
If you need to install other jdks (JDK 7 or JDK 6), you can use homebrew-cask-versions:
$ brew tap caskroom/versions # Install cask, if the cask is already installed, you can omit it. $ brew cask install java6 # Use cask to install other tools
$ /usr/libexec/java_home -V # View the locally installed java version
Then the question is, which JDK is used when you run java or Java programs? Under OS X, java, that is, /usr/bin/java, points to the latest version that has been installed by default. But you can set the environment variable JAVA_HOME to change its pointer
# Check the current java version $ java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27)Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)# Switch version, you can switch to any of the local java homes in the third step. $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home java -version java version "1.6.0_65"Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)
Modify system environment variables:
Add the following content to the ~/.bash_profile (if it is Zsh, modify ~/.zshrc) file:
# JDK 6 export JAVA_6_HOME="/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"# JDK 8export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home"export JAVA_HOME=$JAVA_8_HOME #Default JDK 8#alias command dynamically switches the JDK version alias jdk6="export JAVA_HOME=$JAVA_6_HOME" alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
Update configuration:
$ source ~/.bash_profile #Zsh should be changed to source ~/.zshrc
Switch the java version:
$ jdk6 #Use jdk6$ java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)$ jdk8 #Use jdk8$ java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
Description: The environment variables of the Mac system are loaded in the order of:
/etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc