“This is the 17th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

JDK

The Java Development Kit (JDK), or Java Development Kit, is a free software Development Kit released by Java developers. The JDK usually includes the JRE (Java Runtime Environment), so we need to use the Java Environment.

Generally, when we need to use Java VIRTUAL machine based operation services, we need JDK environment, such Java has two characteristics (Write once, run anywhere) :

  • Easy portability: All you need is a Java virtual machine (JDK) environment to run Java programs.
  • The platform must have Java virtual machine support: Java programs are limited and must be supported by the Java environment

Most developers, or casual users, are familiar with JDK configuration. But:

  • How to configure the JDK on a Linux server?
  • Is it possible to configure and install multiple JDK versions at the same time?

In this tutorial, install multiple versions of the JDK on Linux (demonstrated on Tencent Cloud Lightweight application server) and use Jenv to manage the JDK.

Install the train of thought

A traditional (or regular) installation would be to append the JAVA_HOME file to the environment variable file, and then reload the configuration so that the JDK is ready to use:

The method in this article is similar, but after the normal installation method, append Jenv configuration to implement the JDK switch:

Tutorial synchronization

This tutorial is published simultaneously to:

  • Tencent Cloud Computing

The JDK choice

Before configuring the JDK, let’s take a look at how to select it. There are many different versions of the JDK, generally divided into:

  • OpenJDK: Open source JDK
  • OracleJDK: JDK developed by Oracle corporation

This is a matter of personal preference, but if you ask me, I prefer OpenJDK to avoid copyright disputes. (Every programmer has a dream of making a project bigger and stronger…)

Among them, the SOFTWARE package manager JDK, is also a kind of OpenJDK.

OpenJDK

The JDK was originally developed by SUN, but was later acquired by Oracle. The License protocol for the OracleJDK has been changed from BCL to OTN, which means that you can no longer use this version in production (it may have to be removed 🥲).

JDK17 has changed its license again. This Oracle JDK license is free for all users, even for commercial and production use. Redistribution is allowed as long as there is no charge. OpeJDK, however, remains free.

Though? At the Java One conference in 2006, Sun (before it was acquired by Oracle) announced that Java technology was open source, leading to the birth of OpenJDK, which has been used by more and more people since Google’s Android fully adopted OpenJDK.

At present, OpenJDK basically meets all development requirements and needs, a small part of the need to use the OracleJDK package, the basic can also refer to external packages and write their own dependency packages to solve (such as: Base64 under the Sun package). Therefore, using OpenJDK is a good choice for both development and production environments.

JDK version selection

How to choose the JDK version? In general, the higher the JDK version, the better. The JDK is typically a stable long-term support release, followed by two rapid development iterations and a stable long-term release. For example: JDK8 release, JDK9, JDK10 is the development version, after the JDK11 is a stable long-term support version.

So:

In general, it is better to use stable long-term support versions such as JDK8 and JDK11.

Both Spring Framework 6 and Spring Boot 3 will be based on JAVA 17, and for now, it’s probably better to use JDK8 or JDK17.

ZuluJDK

Decide to use OpenJDK and the JDK version, and then download it? Here I recommend ZuluJDK (a release of OpenJDK) :

Advantages of using ZuluJDK:

  • Supports ARM and X86 architecture devices
  • Supports Windows/Linux/macOS platforms
  • Support JDK6- Latest JDK version
  • Support for configuring JavaFx (Graphical package for Java)

Of course, if you need Oracle JDK, you can download to Oracle’s website: www.oracle.com/java/techno…

Auxiliary video

Hey hey, handy, made a tutorial video:

www.bilibili.com/video/BV1Yw…

Configure the JDK

First time Linux users (especially Linux server users) or macOS users often install directly with software project manager:

# Ubuntu/Debian
sudo apt-get install default-jdk
# Centos
yum install default-jdk
Copy the code

However, the version of OpenJDK installed in this way is not easy to control, and the ZuluJDK of the above size cannot be installed.

Therefore, you are advised to manually configure JAVA_HOME.

Of course, if you want to uninstall the PACKAGE manager JDK, you can:

# Ubuntu/Debian
sudo apt-get purge "java*"
# CentOS
rpm -e --nodeps `rpm -qa | grep java`
Copy the code

JAVA_HOME

As on Windows, we need to configure JAVA_HOME, and therefore the JDK environment. Very simple.

1. Download the JDK

First, select the JDK we want to configure (the JDK here is the system default JDK). For example, here is my x86 architecture server configuration:

  • tar.gz: JDK compressed archive file, which needs to be directly configured to environment variables after decompression.
  • deb: Package manager installation package for the Debian system
  • rpm: Package manager installation package, applicable to CentOS systems.

This configuration JDK, use tar.gz package for configuration.

We directly copy the download address, use a terminal to connect to the server, and then do a quick setup:

# create a new folder for storing JDK (easy to manage) mkdir myEnvironment # go to the folder CD myEnvironment # download JDK17
wget "https://cdn.azul.com/zulu/bin/zulu17.32.13-ca-fx-jdk17.0.2-linux_x64.tar.gz"
Copy the code

2. Release the file

After downloading, is a compressed package file, we need to decompress.

If you haven’t decompressed yet, check out this article: Shell on Linux/macOS: tar and zip commands

Unzip the fileThe tar - xf zulu17.32.13 - ca - fx - jdk17.0.2 - linux_x64. Tar. GzCopy the code

To make the configuration file more convenient and make it easier for users to understand what the file is :), we rename it:

Rename a long list of folders to ZuluJDK16Mv zulu17.32.13 - ca - fx - jdk17.0.2 - linux_x64 ZuluJDK17# Remove the original zip package to solve the Linux server spaceRm - rf zulu17.32.13 - ca - fx - jdk17.0.2 - linux_x64. Tar. GzCopy the code

Now, the preparatory work is complete. The final step: configure the environment variables.

3. Environment variables

First, clarify the location of our JDK:

For example: I is: / root/myEnvironment/ZuluJDK17

Now, the final step is to configure the JDK into an environment variable. Depending on your Shell version, choose:

# Bash Shell user
vim ~/.bashrc
# ZSH Shell user
vim ~/.zshrc
Copy the code

P.S: Generally Linux uses Bash as the default Shell

Additional content:

# JAVA_HOME remember to change it to your own
JAVA_HOME=/root/myEnvironment/ZuluJDK16
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin/
export PATH JAVA_HOME CLASSSPATH
Copy the code

Press ESC, enter :wq, and press Enter to save the configuration. Finally, enter:

# Bash user
source ~/.bashrc
# ZSH user
source ~/.zshrc
Copy the code

After that, we can use our (default) JDK:

java -version
Copy the code

Of course, this only installs one version of the JDK, how to install and configure more versions of the JDK?

Multi-version JDK management

A version of the JDK is installed, but sometimes the Springboot package project cannot start because the JDK version is not compatible:

How do I install multiple versions of the JDK?

There are many ways, and I recommend using Jenv to manage the JDK.

Jenv

Jenv project address: https://www.jenv.be

JEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable

Simply put: You can easily configure JAVA_HOME to environment variables with a single command.

How to configure

Jenv’s configuration is simple: Download Jenv- Configure to Environment variables – reload environment variables

1) download jenv

You can download the Jenv source code directly, or use Git to synchronize the source code directly, for example:

Git clone "Git clone" ://github.com/jenv/jenv.git ~/.jenv
Copy the code

If your server cannot connect to Git, try Gitee’s sync source, or go to jenv’s project address and manually download Jenv and upload it to your server.

2.Configure to environment variables

After that, we can configure jenv in the environment variable:

# Bash Shell user
vim ~/.bashrc
# ZSH Shell user
vim ~/.zshrc
Copy the code

And add:

# Jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
Copy the code

Remember to press Save.

3.Overloading environment variables

Finally, we override the environment variable:

# Bash Shell user
source ~/.bashrc
# ZSH Shell user
source ~/.zshrc
Copy the code

Jenv is now ready to use:

jenv help
Copy the code

Next, let’s focus on the use of JenV.

Jenv specific usage

It’s easy to use Jenv, so here’s how Jenv adds new JDK versions and switches and manages the JDK.

1. The new JDK

I downloaded JDK11 using the method I used to configure JDK16:

The corresponding server storage address is: / root/myEnvironment/ZuluJDK11

It’s easy to add the JDK:

jenv add /root/myEnvironment/ZuluJDK11
Copy the code

Of course, use the same method to add JDK16:

At this point, we have all the required JDK versions installed. Just switch JDK versions.

2. Switch the JDK

The way to switch is simple: first look at the current JDK version installed:

jenv versions
Copy the code

If you don’t have the JDK version you want, just install it as in the previous step.

At this point, we still use the default JDK** (system (set by /root/.jenv/version)) **, we want to switch to JDK11 just need:

jenv local 11
Copy the code

The JDK used in this directory is JDK11:

P.S: If you need the global JDK, use:

jenv global 11
Copy the code

3. Manage the JDK

Sometimes jenv’s Add scan is too much to scan. For example, I only added JDK11 and JDK16, and there are so many JDK versions:

In this case, it is easy to use the remove command:

END

Configuring the JDK and switching between JDK versions on Linux is as simple as that. To be honest, this article is low-tech and focuses on Java configuration and Jevn usage.

However, the foundation of development tools must be good, the high buildings behind the good construction.