Mac manages JDK environment variables

JDK download and install

Java Downloads official website

On the above official website page, we can download the latest JDK19or17, or download the current mainstream JDK8or11.

Select the JDK version and computer version you need, download and install it.
If Mac chooses dmg installation, it will automatically configure the environment variables. If you choose the compressed package, you need to configure the environment variables after decompressing.

Multi-version JDK environment

Taking bloggers as an example, JDK11 is generally used to develop Android programs; but only JDK8 can be used to make cocos2.4.9 game packaging environment.
Therefore, we need to configure a multi-version JDK environment by brew+jenvimplementing version switching.

Jenv is similar to pynev and can switch the JDK environment, but it cannot install JDK. You need to download and install multiple versions yourself and add them with the add command.

brew

brew github home page

Install brew to see brew official website home page

jenv

jenv official document
jenv github homepage

  1. Install jenv using brewbrew install jenv
  2. Configure environment variables
#Bash
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
#Zsh
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
  1. Java Downloads download official website to install different versions of JDK
  2. Add JDK to jenvjenv add PATH_TO_JVM_HOME
    1. The JDK installed by dmg is in the default /Library/Java/JavaVirtualMachines/location and can be jenv add /Library/Java/JavaVirtualMachines/jdk_version/Contents/Home/added.
    2. Add the default jdk of AndroidStudio as:jenv add /Applications/Android\ Studio.app/Contents/jbr/Contents/Home
  3. View the JDK version added to jenv jenv versions, * is the currently selected version by default.
* system (set by /Users/user/.jenv/version)
  11.0
  11.0.2
  openjdk64-11.0.2
  1. Switch to the specified version
jenv shell <version>-- 只为当前 shell 会话选择
jenv local <version>-- 当你在当前目录(或其子目录)时自动选择
jenv global <version>-- 为您的用户帐户全局选择

jenv globalThe terminal needs to be restarted after switching.

Guess you like

Origin blog.csdn.net/DeMonliuhui/article/details/128460524