一、概述
原理:动态修改JAVA_HOME变量。
二、脚本
临时生效:
vim ~/.bash_profile
### JDK definition
JAVA_08="/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home"
JAVA_11="/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home"
JAVA_17="/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
### alias
alias j8='export JAVA_HOME=$JAVA_08 && echo "Switch to Java8"'
alias j11='export JAVA_HOME=$JAVA_11 && echo "Switch to Java11"'
alias j17='export JAVA_HOME=$JAVA_17 && echo "Switch to Java17"'
source ~/.bash_profile
永久生效:
vim ~/.bash_profile
### JDK definition
JAVA_08="/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home"
JAVA_11="/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home"
JAVA_17="/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
### alias
alias j8='sed -i "" "/export JAVA_HOME=\//d" ~/.bash_profile && echo "export JAVA_HOME=$JAVA_08" >> ~/.bash_profile && source ~/.bash_profile && echo "Switch to Java8"'
alias j11='sed -i "" "/export JAVA_HOME=\//d" ~/.bash_profile && echo "export JAVA_HOME=$JAVA_11" >> ~/.bash_profile && source ~/.bash_profile && echo "Switch to Java11"'
alias j17='sed -i "" "/export JAVA_HOME=\//d" ~/.bash_profile && echo "export JAVA_HOME=$JAVA_17" >> ~/.bash_profile && source ~/.bash_profile && echo "Switch to Java17"'
alias jv='java -version'
source ~/.bash_profile
三、使用
j8
j11
j17
jv