If you run a Java command on a MacOS that does not have JDK installed, the following prompt will appear:

AigeStudio@aige$ java -version
No Java runtime present, requesting install.
Copy the code

MacOS will then prompt you that the JDK is not installed:

Clicking on more information takes you to the Oracle JDK download page for you to download and install. Clicking on more information on earlier Versions of MacOS takes you to the Apple JDK 6 download page. Either way, MacOS requires you to install the JDK yourself. Although it is feasible to download and install JDK by yourself, the JDK package is not app but PKG form, so it is very inconvenient to manage, upgrade and uninstall JDK. Therefore, it is not recommended to download and install JDK by yourself. In contrast, brew provides automatic installation and uninstall functions. Management is also easier than their own installation. Install BREW and configure CASK in MacOS if you have not already used brew.

Installing the JDK using BREW is also very simple. If you just want to install the latest JDK, simply run the following command:

AigeStudio@aige$ brew cask install java
Copy the code

If you use BREW to install other versions of the JDK, you need to use the “homebrew-cask-versions” command line tool, which is used to install historical versions of the application. If you haven’t used it before, you need to configure it with the following command:

AigeStudio@aige$ brew tap caskroom/versions
Copy the code

Then execute brew’s search command to see what JDK versions are available for installation:

AigeStudio@aige$ brew search java
Copy the code

Iterm2&zsh: iterm2&oh-my-zsh: iterm2&zsh: iterm2&oh-my-zsh: iterm2&zsh: iterm2&zsh: oh-my-zsh: iterm2&zsh: oh-my-zsh: iterm2&zsh: oh-my-zsh: iterm2&zsh: oh-my-zsh: iterm2&zsh: oh-my-zsh:

To view all installed JDK versions, run the following command:

AigeStudio@aige$ /usr/libexec/java_home -V
Copy the code

For example, there are three versions of the JDK installed (JDK6 differentiates the 32bit and 64bit versions) :

Once we have each different JDK path, we can start switching between them. By default, Java on MacOS uses the latest JDK version, using the following command:

AigeStudio@aige$ java -version
Copy the code

The latest version is Open JDK 12:

We can switch between JDK versions by setting the system environment variable JAVA_HOME. The simplest way to change its value manually is through the “export” command:

AigeStudio@aige$ exportJAVA_HOME = / Library/Java/JavaVirtualMachines / 1.6.0 JDK/Contents/Home
Copy the code

However, typing such a long list of commands every time is too tedious and not smart, we can use the shell configuration file (~/.bash_profile if it is bash; If ZSH, simplify the switch command by specifying alias in ~/.zshrc) :

# JDK 6, the JDK 8, the export command to export the JDK 12 JDK6_HOME = "/ Library/Java/JavaVirtualMachines / 1.6.0 JDK/Contents/Home" export JDK8_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home" export JDK12_HOME = "/ Library/Java/JavaVirtualMachines/its - 12.0.1. JDK/Contents/Home" # alias command link to the export command alias jdk6="export JAVA_HOME=$JDK6_HOME" alias jdk8="export JAVA_HOME=$JDK8_HOME" alias jdk12="export JAVA_HOME=$JDK12_HOME"Copy the code

After modifying the Shell configuration file, save it and run the source command to update the configuration file in real time:

AigeStudio@aige$ sourceConfiguration file path (~/.bash_profile if bash; If ZSH, ~/.zshrc)
Copy the code

Finally we can switch JDK versions in Shell using jdk6, jdk8, jdk12 commands.

Using export & Alias to manage the JDK is fine, but it doesn’t work when dealing with complex scenarios. For example, we have many different Java projects that depend on each other, and none of them use the same JDK version. If we switch the JDK using export & Alias, it is possible that some projects will compile successfully and others will not. A better idea is to limit the scope of export & Alias so that its setting of JDK environment variables can be applied to a specific project, that is, to a specific directory. Although this idea is good, it is more difficult to implement it using export & alias. JEnv is a widget that helps you manage and switch between JDK versions.

JEnv is easy to install, and you can refer directly to its official website. JEnv currently supports both Linux and MacOS, on which you can install directly using BREW (note: if you haven’t used BREW, see Installing BREW and configuring CasK on MacOS) :

AigeStudio@aige$ brew install jenv
Copy the code

Then configure the Shell and add the following configuration parameters

#If you are using bash, execute the following command
AigeStudio@aige$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
AigeStudio@aige$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

#If you are using ZSH, execute the following command
AigeStudio@aige$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
AigeStudio@aige$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
Copy the code

The /usr/libexec/java_home -v command is used to list the installed JDK paths:

AigeStudio@aige$ /usr/libexec/java_home -VMatching Java Virtual Machines (4): 12.0.1, x86_64: "Its 12.0.1"/Library/Java/JavaVirtualMachines/its - 12.0.1. JDK/Contents/Home 1.8.0 comes with _212, x86_64: "AdoptOpenJDK 8"/Library/Java/JavaVirtualMachines/AdoptOpenJDK - 8. The JDK/Contents/Home 1.6.0 _65 - b14-468, x86_64: "Java SE 6"/Library/Java/JavaVirtualMachines 1.6.0. JDK/Contents/Home 1.6.0 _65 - b14-468, i386: "Java SE 6"/Library/Java/JavaVirtualMachines 1.6.0. JDK/Contents/HomeCopy the code

Use the jenv add command to add the paths one by one:

AigeStudio@aige$Jenv add/Library/Java/JavaVirtualMachines/its - 12.0.1. JDK/Contents/Home
AigeStudio@aige$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
AigeStudio@aige$Jenv add/Library/Java/JavaVirtualMachines / 1.6.0 JDK/Contents/Home
Copy the code

After the configuration is complete, run the versions command of JEnv to view the JDK versions that have been added to JEnv management:

AigeStudio@aige$ jenv versions1.6 1.6.0.65 1.8.0.212 1.8 12.0 * 12.0.1 system (set by/Users/aige /. Jenv/version) openjdk64-1.8.0.212 openjdk64-12.0.1 Oracle64-1.6.0.65Copy the code

If you see that the JDK 6, JDK 8, and JDK 12 that we added have been added, the configuration is successful. Note here that JEnv reads the different aliases of the JDK for display. For example, the 1.8, 1.8.0.212, and OpenJDk64-1.8.0.212 listed on the command line above refer to JDK 8. The asterisk “*” to the left of the version number indicates the JDK version that will be used after the Java command is executed. For example, with an asterisk to the left of 12.0.1 on the command line above, we execute a Java version command:

AigeStudio@aige$ java -versionOpenjdk Runtime Environment (build 12.0.1+12) OpenJDK 64-bit Server VM (build 12.0.1+12 12.0.1 + 12, mixed mode, sharing)Copy the code

It will display the version of JDK 12.

The essence of JEnv is the shell, local, and global parameter commands. The shell is used to set the JDK version to use during the terminal window lifecycle; Local is used to set the JDK version used in the current directory. Global is used to set the JDK version used globally. All three commands are used in the same way:

AigeStudio@aige$ jenv shell/local/ global 1.6
Copy the code

The “1.6” at the end of the above command is the alias for the different JDK versions we set in JEnv. Here, if we want to use JDK 8 in the current terminal window, we simply execute:

AigeStudio@aige$Jenv shell 1.8
Copy the code

The Java version command will display JDK 8:

AigeStudio@aige$Jenv shell 1.8
AigeStudio@aige$ java -versionOpenjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-b04) OpenJDK 64-bit Server VM 25.212 b04 (AdoptOpenJDK) (build, mixed mode)Copy the code

Shell commands set the JDK to be valid for the lifetime of the terminal window, meaning that once you exit the terminal and re-enter it, it will be invalid. The local command lets you execute a specific version of the JDK in a directory:

The local command doesn’t have a life cycle like the shell. Once you set the JDK version in a directory using local, a “.java-version “file is generated in that directory. This file records the JDK version used in the current directory. The Settings remain valid. The final global command is omitted and is easy to understand.

The shell, local, and global commands are pretty much everything JEnv has, but there are a few other features that are not very common. You can refer to the official documentation or check out JEnv’s command help:

AigeStudio@aige$ jenvJenv 0.5.2 Usage: jenv <command> [<args>] Some Useful jenv commands are: commands List all available jenv commands local Set or show the local application-specific Java version global Set or show the global Java version shell Set or show the shell-specific Java version rehash Rehash jenv shims (run this after installing executables) version Show the current Java version and its origin versions List all Java versions available to jenv which Display the full path to an executable whence List all Java versions that contain the given executable See  jenv help <command> for information on a specific command. For full documentation, see: https://github.com/hikage/jenv#readmeCopy the code