the first method:
Easily switch to the corresponding Java version with the commands 'jdk6', 'jdk7', 'jdk8':
1. First install all JDk:
* The JDK6 that comes with Mac is installed in the directory: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/.
* JDK7, JDK8 need to download and install the corresponding version from Oracle official website. The default path of the JDK installed by yourself is: /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk
2. Configure
to create a .bash_profile configuration file (skip this step if you already have this file)
1
|
touch
~/.bash_profile
|
#vimEdit .bash_profile file
1
|
vim ~/.bash_profile
|
#If you are not used to the vim command, use the built-in text editor to open it
1
|
open
~/.bash_profile
|
set jdk version
1
2
3
4
5
|
export
JAVA_6_HOME=
/System/Library/Java/JavaVirtualMachines/1
.6.0.jdk
/Contents/Home
export
JAVA_7_HOME=
/Library/Java/JavaVirtualMachines/jdk1
.7.0.jdk
/Contents/Home
export
JAVA_8_HOME=
/Library/Java/JavaVirtualMachines/jdk1
.8.0.jdk
/Contents/Home
export
JAVA_HOME=$JAVA_8_HOME //set default use
|
The alias command dynamically switches the configuration of JAVA_HOME
1
2
3
|
alias
jdk8=
'export JAVA_HOME=$JAVA_8_HOME'
alias
jdk7=
'export JAVA_HOME=$JAVA_7_HOME'
alias
jdk6='
export
JAVA_HOME=$JAVA_6_HOME’
|
#Save and execute the following command after the input is completed
#Re-execute the .bash_profile file
1
|
source
~/.bash_profile
|
3. Verify:
Use: jdk6, jdk7, jdk8 to switch jdk version
Note: restart the console to take effect
vim operation and forgot common commands can refer to http://www.yneit.com/?p=393
The second:
http://www.jenv.be/
To use jenv management, first download the homebrew management suite
Question: Note that the jdk version switch has taken effect, but java -version may still display the old version