Extensive and fine management of multiple Java versions under one machine

foreword

In the process of software development, we often encounter "old" projects whose JDK is still in Java 6, Java 7 or even earlier versions. At the same time, when learning new JDK features, it is often necessary to install the latest version of JDK. In view of these situations, we need to install multiple JDKs in the local environment and switch them easily.

This article takes the Mac OS operating system as an example to demonstrate how to install multiple JDK versions and switch them. Here we focus on two methods, the form based on the alias and the form based on the jenv software.

Alias-based JDK switching

Here we take Java 9 under the Mac operating system as an example for demonstration.

JDK installation

Download the dmg installation package of Java 9 under Mac, and follow the steps all the way to Next to install.

After the installation is complete, two (if multiple JDKs are installed) directories will appear in the Mac's /Library/Java/JavaVirtualMachines directory:

jdk-9.0.4.jdk		jdk1.8.0_151.jdk

In addition to the current mainstream Java 8, the new long-term support version (LTS) includes Java 11 and Java 17, and you can follow it according to your needs. The above shows the situation that there are two JDK versions, and other versions can be compared.

Environment variable configuration

Under Mac, the environment variables of JDK can be configured through the bash_profile file. Execute the following command to open the configuration file:

vim ~/.bash_profile

If there is no .bash_profile file, it will be created when running the vim ~/.bash_profile command.

Referring to the original environment variable configuration, add the configuration of JDK9:

# Java config
export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home"
export JAVA_9_HOME="/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home"

# config alias
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk9="export JAVA_HOME=$JAVA_9_HOME"

# config default jdk
export JAVA_HOME=$JAVA_8_HOME
export PATH="$JAVA_HOME:$PATH"

There are two modifications above: the first defines the Home paths of JAVA_8_HOME and JAVA_9_HOME; the second configures the aliases (alias) of jdk8 and jdk9 operations.

Save the modified configuration above, and execute the following command to make bash_profile take effect:

source ~/.bash_profile

After the compilation is completed, the relevant configuration is completed, and the JDK can be switched through commands later.

JDK environment switching

In the above configuration, Java 8 is used by default, which java -versioncan be verified by executing the command:

192:JavaVirtualMachines zzs$ 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)

When you need to switch to Java 9, execute the following command:

192:JavaVirtualMachines zzs$ jdk9
192:JavaVirtualMachines zzs$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

At this point, it has been switched to Java 9. Of course, if you want to switch back to Java 8, just do it jdk8.

Through this form of alias, multiple JDK environments can be configured, but in some complex environments, this simple manual switching seems powerless. We need a more convenient and fine-grained control tool. Here we recommend JEnv.

JEnv tool usage

As the official website says, JEnv is an artifact that makes you forget how to configure JAVA_HOME. You can switch between different Java versions with simple commands. Let's take a look at the installation and basic use of JEnv.

Installation of JEnv

JEnv can be installed using Homebrew under Mac OS:

brew install jenv

After waiting for the above command to be installed successfully, you need .bash_profileto configure it in . The corresponding configuration content is also printed on the console during installation. Here, the content is directly added to .bash_profilethe file through the echo command:

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

After the installation is complete, execute the command to check the version information:

$ jenv versions
* system (set by /Users/zzs/.jenv/version)

It can be seen from the results that only the system default Java is found, and * indicates the currently selected version. Although other versions of Java were installed, they were not auto-discovered.

Still taking the current Java 8 and Java 9 environment as an example, we jenv addadd the corresponding Java path to jenv through the command:

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/
oracle64-1.8.0.151 added
1.8.0.151 added
1.8 added
 1.8.0.151 already present, skip installation

Java 9 added to jenv:

$ jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/
oracle64-9.0.4 added
9.0.4 added
9.0 added
 9.0.4 already present, skip installation

Then run the query version information command:

$ jenv versions
* system (set by /Users/zzs/.jenv/version)
  1.8
  1.8.0.151
  9.0
  9.0.4
  oracle64-1.8.0.151
  oracle64-9.0.4

You can see that the JDKs of Java 8 and Java 9 have been added to jenv. But obviously, some unnecessary JDK versions have also been added, which can be removed by the remove command:

$ jenv remove oracle64-1.8.0.151
JDK oracle64-1.8.0.151 removed

When the unwanted version is removed, check again:

$ jenv versions
* system (set by /Users/zzs/.jenv/version)
  1.8.0.151
  9.0.4

Only two versions 1.8.0.151 and 9.0.4 are left.

Use of JEnv

After completing the above installation, you can use the command to switch the Java version used:

$ jenv local 1.8.0.151
/usr/local/Cellar/jenv/0.5.5_2/libexec/libexec/jenv-version-file-write: line 19: .java-version: Permission denied

When trying to use it, the expected effect did not appear, and a permission problem was reported. Here you can use the doctor command to check the reason:

$ jenv doctor
[ERROR]	JAVA_HOME variable already set, scripts that use it directly could not use java version set by jenv
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded

The first line indicates that the JAVA_HOME variable has been configured, so edit .bash_profilethe file to comment out the original JAVA_HOME variable, and execute again:

$ jenv doctor
[OK]	No JAVA_HOME set
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded

After the above operations, I found that the "Permission denied" problem still occurred when switching the Java version, so I exited the command line window and reopened the problem and it disappeared.

Version before switching:

$ java -version
openjdk version "17.0.4.1" 2022-08-12
OpenJDK Runtime Environment Temurin-17.0.4.1+1 (build 17.0.4.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.4.1+1 (build 17.0.4.1+1, mixed mode, sharing)

Execute the switch, and the version after the 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)

It jenv localspecifies the local Java version in a folder, and you can also jenv globalset a default Java version to run jenv which javaand display the full path of executable Java.

Set the global default version:

jenv global 1.8

Display the full path to the executable Java:

$ jenv which java
/Users/zzs/.jenv/versions/1.8/bin/java

You can also use files in specific folders .java-versionto set the Java version.

$ cat .java-version
1.8

In the directory where the command was executed jenv local 1.8, you can see that the above file is generated .java-version, and the Java version number is stored in the file.

For example, when using Java 8 in a project, I only need to save 1.8 as the content in .java-versionthe file. When I enter this folder, JEnv will automatically help me set the local Java version.

PS: In the process of the above operations, we need to pay attention to some moments when the expected effect is not achieved. It may be a problem with the cache of the command line window. You can try to close the command line window and reopen it for viewing.

JEnv practice

Here, a Java 17-based project java17-learn is created through Idea 2022:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── secbro2
    │   │           └── Main.java
    │   └── resources
    └── test
        └── java

After the creation is complete, enter the project root directory and execute the jenv command to set the environment variable of the corresponding directory to Java 17:

$ jenv local temurin64-17.0.4.1
$ java -version
openjdk version "17.0.4.1" 2022-08-12
OpenJDK Runtime Environment Temurin-17.0.4.1+1 (build 17.0.4.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.4.1+1 (build 17.0.4.1+1, mixed mode, sharing)

You can see that the corresponding Java version has been set for the specified directory. At this point, if you open the project with IDEA, you will see one more .java-versionfile in the root directory of the project, open the file, the content is:

temurin64-17.0.4.1

The newly created project does not conflict with the global default Java 8 environment variable configuration, and they can run normally. So far, the introduction to the use of JEnv has been completed.

summary

This article aims at the management of environment variables in multiple Java versions, and takes you to practice and learn two ways to manage environment variables: one is relatively extensive, based on the form of environment variable command aliases, and the current environment variables are switched through aliases; The other is to use the third-party tool JEnv to complete the precise management of multiple Java versions, which can be refined to specific file directories. If conditions permit, it is recommended that you use JEnv for management.

Guess you like

Origin blog.csdn.net/wo541075754/article/details/127645274