Linux system uses snap to install JDK

Time:2022-5-7

What is snap?

snapIt is a new way of software package management. It is similar to a container. It has all the files and libraries of an application, and each application is completely independent. So the advantage of using snap package is that it solves the dependency problem between applications and makes it easier to manage between applications. But the problem is that it takes up more disk space.
The installation package extension of snap is.snap, similar to a container, it contains all the files and libraries needed by an application (the snap package contains a private root file system, which contains the dependent software packages). They will be installed in a separate directory; Applications are isolated from each other. Using snap has many advantages. Firstly, it solves the problem of software package dependency; Second, it also makes the application easier to manage.
At present, there are not many applications that support snap, and the snap software package is generally installed in/snapDirectory.

How to install snap?

In the Ubuntu system, snap comes with the system by default, but not in the official image of the Ubuntu system in the docker container. Most Linux distributions do not come with snap, so it needs to be installed additionally. Here’s a demonstrationdebianHow to install snap for the distribution. Please refer to this article for the steps of installing snap in RPM distribution versions such as CentOShttps://www.linuxidc.com/Linux/2020-03/162485.htm

Configure the source of APT

defaultubuntuofApt software sourceToo slow access in China often leads to installation failure. Use it heresedCommand pairApt software sourceReplace withAlibaba's apt software source

sed -i [email protected]/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
sed -i [email protected]/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt-get clean
apt-get update

Install snap

sudo apt-get install snapd

Installing JDK using snap

sudo snap install openjdk

Add JDK to environment variable

Careful students may have found that if you use snap to install openjdk, you still can’t run Java related commands in the command, because we haven’t configured the JDK into the environment variable.

Enter openjdk in the command to get the configuration file address of openjdk

openjdk
//For example, output
// /var/snap/openjdk/common/openjdk.env

Use the cat command output to view the contents of the configuration file

cat /var/snap/openjdk/common/openjdk.env

For example, my output here is as follows:

# Source this file for OpenJDK environment variables and aliases
export JAVA_HOME=/snap/openjdk/714/jdk
export MANPATH=/snap/openjdk/714/jdk/man:
alias java='openjdk.java'
alias javac='openjdk.javac'
alias javadoc='openjdk.javadoc'
alias jar='openjdk.jar'
alias jarsigner='openjdk.jarsigner'
alias jlink='openjdk.jlink'
alias jpackage='openjdk.jpackage'

Copy the output of the configuration file withvimOr another editor opens/etc/profileFile (note that sometimes you needsudo), add the copied configuration content to/etc/profileAt the end of the file.

Refresh environment variables

source /etc/profile

At this time, you can view the Java version information in the terminal. If it can be output, the configuration is successful. (you may need to restart the computer in some cases)

java --version

//Output sample
// openjdk 17.0.2 2022-01-18
// OpenJDK Runtime Environment (build 17.0.2+8-snap)
// OpenJDK 64-Bit Server VM (build 17.0.2+8-snap, mixed mode, sharing)