本文主要講述一下命令行一鍵切換java版本的幾種方式java
java如今版本發佈相對之前比較頻繁了,java9還沒來得及用,java10就出來了,2018年9月份java11也要來了,因而就有需求在筆記本電腦上裝多個版本的java,一鍵切換就變得極爲迫切。node
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 }
值得注意的是這個命令只對當前命令窗口有效。新開的窗口仍是使用~/.zshrc裏頭原來配置的JAVA_HOME版本。
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有點相似node的nvm之類的用來管理多版本
brew install jenv echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc echo 'eval "$(jenv init -)"' >> ~/.zshrc mkdir -p ~/.jenv/versions
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
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
➜ ~ 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)
這個local做用當前窗口以及以後新開的窗口
相對於local,有個global,能夠用來設置默認的版本,不過我這邊貌似不起做用,先忽略這個
jenv enable-plugin maven
jenv的這個命令能夠強制maven使用jenv配置的java版本
切換java版本的本質仍是修改JAVA_HOME環境變量,須要注意切換後該環境變量的做用域,jenv能夠做用到以後新打開的窗口。git