How to make the JDK version switch as smooth as silk

I use Java 8 at work, but I have two versions of JDK installed on my personal computer: openjdk8 and openjdk11. There was a problem when using jconsole just now, and switching to jdk8 is OK. Manually adjust Java every time_ Home is too much trouble. We can at ~ / bash_ The profile is configured as follows:

export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home"
export JAVA_11_HOME="/Library/Java/JavaVirtualMachines/jdk-11.0.6+10/Contents/Home"
export JAVA_HOME=$JAVA_8_HOME
alias java8="export JAVA_HOME=$JAVA_8_HOME"
alias java11="export JAVA_HOME=$JAVA_11_HOME"

In the above configuration, the first two lines export the environment variables of java8 and java11, and the third line sets the default Java home to java8. The alias is configured in the last two lines. After configuration, execute source ~ / bash_ Profile makes the configuration take effect. If you use Zsh, you can execute VIM ~ / Zshrc and add the following configuration at the end of the file:

source ~/.bash_profile

Let's verify:

$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16,mixed mode)

The default value is java8. No problem. Execute the command java11 to switch to 11. See below:

# root @ localhost in ~ [23:52:38] 
$ java11

# root @ localhost in ~ [23:52:40] 
$ java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10,mixed mode)

No problem. Let's switch back to java8 and execute the command java8 as follows:

# root @ localhost in ~ [23:52:49] 
$ java8        

# root @ localhost in ~ [23:54:04] 
$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16,mixed mode)

If you don't match it, match it quickly!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>