How do I configure multiple versions of Java under Mac

  • 2021-01-19 22:16:37
  • OfStack

instructions

Tools: brew cask

brew cask is a command line management tool for ES11en applications. It provides automatic installation and uninstallation functions. It can automatically download and install the latest version of brew from the official website.

1. Install the latest version of Java


#  How not to install  brew cask . Please perform  $ brew tap caskroom/versions
$ brew cask install java

2. Install other versions of Java

If you need to install another jdk(JDK 7 or JDK 6), you can use homebrew-cask-versions:


$ brew tap caskroom/versions #  The installation cask , if already installed cask You can omit it. 
$ brew cask install java6 #  use cask Install other tools 

3. Check the locally installed Java Home


$ /usr/libexec/java_home -V # View the locally installed java version 

4. Switch to java version [manually modify environment variables]

Which JDK do you use when running java or Java programs? Under OS X, java /usr/bin/java by default refers to the latest version that has been installed. However, you can set the environment variable JAVA_HOME to change the pointing


#  View 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, can be switched to the first 3 Step of the local java home Any of the 1 A. 
$ 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)

5. The configuration command switches automatically

Modify system environment variables:

Add the following to the file ~/.bash_profile (if it is Zsh, modify ~/.zshrc) :


# JDK 6 
export JAVA_6_HOME="/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"
# JDK 8
export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home"

export JAVA_HOME=$JAVA_8_HOME # The default JDK 8

#alias Command dynamic switch 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 to 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: Mac system environment variables, the loading order is:
/etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc

]


Related articles: