Order
This article mainly tells about several ways of switching java version with one-click command line.
Background
Java releases are more frequent now than before. java10 came out before java9 could be used, and java11 will come in September 2018. Therefore, there is a need to install multiple versions of java on notebook computers, and one-click switching becomes extremely urgent.
Zsh function implementation
~/.zshrc
function jvm {
version=$1
case "$version" in
9)
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home'
;;
10)
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home'
;;
*)
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home'
;;
esac
}
It is worth noting that this command is only valid for the current command window. The newly opened window still uses the version of JAVA_HOME originally configured in ~/.zshrc.
Use instance
jvm 8
java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
jvm 9
java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
jvm10
java -version
openjdk version "10" 2018-03-20
OpenJDK Runtime Environment 18.3 (build 10+46)
OpenJDK 64-Bit Server VM 18.3 (build 10+46, mixed mode)
jenv
Jenv is somewhat similar to nvm of node for managing multiple versions.
Installation
brew install jenv
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
mkdir -p ~/.jenv/versions
Register java version
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
View version
jenv versions
* system (set by /Users/demo/.jenv/version)
1.8
1.8.0.151
10
9
openjdk64-10
oracle64-1.8.0.151
oracle64-9
switch
➜ ~ jenv local 1.8
➜ ~ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
➜ ~ jenv local 9
➜ ~ java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
➜ ~ jenv local 10
➜ ~ java -version
openjdk version "10" 2018-03-20
OpenJDK Runtime Environment 18.3 (build 10+46)
OpenJDK 64-Bit Server VM 18.3 (build 10+46, mixed mode)
This local acts on the current window as well as the newly opened windows
Compared with local, there is a global, which can be used to set the default version, but my side does not seem to work, so ignore this first.
maven
jenv enable-plugin maven
This command from jenv can force maven to use the java version configured by jenv.
Summary
The essence of switching java version is to modify JAVA_HOME environment variable. Attention should be paid to the scope of the environment variable after switching. jenv can be applied to the newly opened window.