CentOS-7 download, install and configure Java-19 (including uninstallation and installation)

CentOS-7 download, install and configure Java

Version number: CentOS-7-x86_64-Minimal-2009

Windows version: Windows 11 Home Chinese version 22H2

Vmware版本:VMware® Workstation 17 Pro - 17.0.1 build-21139696

Version information of the virtual machine

Installed Java version: jdk-19

First of all, there are three methods here, one is the method of rpm download, the other is the method of yum, and the last is the method of downloading to the host and then uploading to the virtual machine for decompression (using tar).

insert image description here

Looking at the picture, it is not difficult to find that the minimal installation comes with rpm, but not wget, so if you want to use rpm, you have to download the wget tool first, or directly upload it from the host to the virtual machine remotely.

1. Update and upgrade the CentOS with minimal installation

Update installs the latest version of existing software and security upgrades, and does not install any new software. In general update (update) and upgrade (upgrade) are the same, but in fact upgrade = update + update is discarded.

sudo yum update && yum upgrade

insert image description here

But because there may be a lot of software packages, and the system prompts to accept changes, we generally use the following command, which will not pop up a software update prompt, and you don't need to enter 'y' to accept the changes.

sudo yum update -y && yum upgrade -y

Notice

But while learning about virtual machines only, it's always a good idea to see what changes will happen on the server when you're actually developing in production, especially in production. Therefore, although using the above command can automatically update and upgrade for you, it is not recommended.

Two, rpm install jdk

1. Download the wget tool

As mentioned earlier, the minimally installed CentOS7 does not have its own wget tool, so we need to download it

sudo yum install wget -y

insert image description here

After downloading, we can use the wget tool to download and save to the specified directory.

2. Use wget to download the jdk installation package

wget https://download.oracle.com/java/19/latest/jdk-19_linux-aarch64_bin.rpm -P ~/Downloads/

In the picture below, I typed -P, so it cannot be saved to the specified directory later. You can see the red frame where it is saved. There is a small mistake, but the problem is not big.

insert image description here

One advantage of using rpm to install is that it is installed in the /usr/java directory by default, without configuring environment variables, which can save a lot of trouble. If you use decompression, you need to configure environment variables, and there may be a little problem. .

Here I encountered a small problem... that is, I read the wrong version, I should download the x64 compressed package, so I downloaded the wrong one, a little stupid, so I just read it clearly when downloading, otherwise it will be like me Same, have to download again.

insert image description here

wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.rpm -P ~/Downloads/

This time I remembered to add that -P, and you can see that the saved location is the same as I imagined.

insert image description here

insert image description here

3. Install with rpm

There is nothing special about the installation command, simply

 sudo rpm -ivh Downloads/jdk-19_linux-x64_bin.rpm

Here we have encountered another warning, this warning is because there are always some dependencies between various software, so the warning will be issued.
insert image description here

Here I checked the installed java version. In theory, because there are warnings, there are no dependencies, so the installation should not be successful, but later I found that even if there are warnings, it can be installed, which is very strange.

insert image description here

It doesn't matter, here I took a snapshot to restore the previous situation, and then downloaded it again, and then when it was installed, we only need to add –force --nodeps at the end, nodeps means to ignore dependencies. Because there will be more or less connections between various software. With these two setting options, these dependencies are ignored, forced to install or uninstall

sudo rpm -ivh Downloads/jdk-19_linux-x64_bin.rpm  --force --nodeps

kinda... embarrassingly, he's still there, kinda shocks me

insert image description here

The most important thing is...it...can be found again...this is a bit strange, but it doesn't affect it, let's leave the question first, because it is not an error, it will not affect the use...(mainly because I don't know how to solve it)

insert image description here

But the most important thing is that it installed successfully, which means it doesn't matter.

Three, tar install jdk

As usual, because the wget tool is not downloaded, it needs to be downloaded, but it has been mentioned above, so it is omitted.

1. wget download jdk installation package

Oh, I forgot to mention, we downloaded java 19, if you need other versions, please go to Oracle official website to find another one.

wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz -P Downloads/

insert image description here

2. Tar decompresses the jdk_19 compressed package

After downloading, we use the tar command to decompress it into the java folder under usr

sudo tar -zxvf  Downloads/jdk-19_linux-x64_bin.tar.gz -C /usr/java/

insert image description here

But because there is no java folder in it, you need to create a folder first, and then decompress it.

sudo mkdir /usr/java/

insert image description here

Execute the decompression command again to decompress, and the following effect shows that the decompression has been successfully completed.

insert image description here

3. Configure environment variables

Method 1: Add initial environment variables

Because we just decompressed the compressed package in the simplest way, it is still not usable. When we enter java --version to check the java version, we can't check it out.

java --version

insert image description here

This is because we haven't configured environment variables for him, which is the advantage of rpm compared to tar, but it is not particularly difficult to configure environment variables. But before configuring environment variables, we need to know some things. Since java_12, jre has been merged with jvm, that is to say, there is no jre in our environment variables. So if you are using a version earlier than java_12, you need to configure jre environment variables.

insert image description here

To configure environment variables, first of all, you need to find the file where the environment variable is located, then modify the content of the file, and finally restart the environment variable service. The environment variable file is in /etc/profile, so we use vim /etc/profile to modify it

sudo vim /etc/profile

Below we use jdk_1.8 as an example, and write the following to the end of the environment variable file.

JAVA_HOME=/usr/java/jdk1.8.0_161

JRE_HOME=/usr/java/jdk1.8.0_161/jre

PATH= P A T H : PATH: PATH:JAVA_HOME/bin:$JRE_HOME/bin

CLASSPATH=.: J A V A H O M E / l i b / d t . j a r : JAVA_HOME/lib/dt.jar: JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

export JAVA_HOME JRE_HOME PATH CLASSPATH

Because I downloaded jdk-19, the content of the file I configured is also different, as follows

#JAVA
JAVA_HOME=/usr/java/jdk-19.0.2/
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH

After configuration, the environment variables must be reloaded

source /etc/profile

At this time, when we check the java version, it will be displayed, which means that our installation has been successful.

insert image description here

Method 2: Add temporary environment variables

Because in the /etc/profile.d (not the same thing as the above, this is the file above the directory) directory is the startup script required by some applications, including color, language, less, vim and Some additional settings for commands such as which. The reason why these script files can be executed automatically is because a for loop statement is used in /etc/profile to call these scripts. And these script files are used to set some variables and run some initialization process.

insert image description here

So we also have a second method, as an additional temporary environment variable, to avoid polluting the initial environment variable. That is to create a java.sh file in this directory, and then add environment variables, so that the initial environment variables can not be polluted.

First, create a new java.sh under /etc/profile.d/

sudo vim /etc/profile.d/java.sh

Then, write configuration environment variables

#JAVA
JAVA_HOME=/usr/java/jdk-19.0.2/
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH

Finally exit and save. reload environment variables

source /etc/profile

At this point, you can download and install jdk completely.

Fourth, yum download and install jdk

First of all, we have to confirm which version of jdk yum can download, so we use the following command to check which jdk exists in the yum library

yum list | grep jdk

insert image description here

It's a pity that the latest version here is only up to java-11, so just choose java-11 to download and install.

sudo yum -y install java-11-openjdk.x86_64

insert image description here

insert image description here

It's done here, and then let's take a look at the java version, and check whether the environment variables have been loaded by the way.

java --version

insert image description here

So here it is downloaded and installed.

Five, uninstall jdk

Although we have learned how to download and install, if we use the version we downloaded and installed ourselves (especially using the tar method to configure environment variables) when updating the version, we need to uninstall jdk first and then Install the new version.

1. View the jdk installation package name

First of all, we have to confirm whether there is already downloaded and installed jdk

rpm -qa |grep jdk
rpm -qa |grep java
rpm -qa |grep gcj

Here are a total of three statements for you, and there is always one you can find (of course, what we uninstalled is the three files in the first statement, don’t delete the one with python, you can figure it out if you delete it), If you can't find it, it means that your jdk is not downloaded and installed with rpm or yum.

insert image description here

If you really can't find it, use another command to find it (please make sure you really have jdk installed!!!)

which java

insert image description here

You should be able to find it at this time, or you have not configured environment variables, etc. If you don't have jdk installed, you will never find it in your life. Please don't trouble me at this time, and I can't help you.

Maybe the brothers have other ideas, such as the following command:

which jdk

insert image description here

It's not that this is not possible, but you see, there is no jdk option, so try not to do this, and use the which java.

2. Uninstall jdk

Then we have two ways to uninstall:

Method 1: rpm uninstall

This uninstallation is not random. This file has a saying, which is the three in the picture below. If you can’t understand the following long command, then you can divide it into three times and delete one by one. The specific command form:

sudo rpm -e --nodeps [文件名]

insert image description here

For convenience, I delete the three together here.

sudo rpm -e --nodeps java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64 copy-jdk-configs-3.3-11.el7_9.noarch java-11-openjdk-headless-11.0.18.0.10-1.el7_9.x86_64

Method 2: yum uninstall

The uninstallation here is relatively simple, just type in the blue frame.

insert image description here

sudo yum -y remove java-11-openjdk-headless-11.0.18.0.10-1.el7_9.x86_64

insert image description here

insert image description here

At this point, we have finished and uninstalled. If you are not at ease, just check whether java exists.

insert image description here

What's interesting is that I found some uninstall residues here again, em... a little bit wrong, but it doesn't affect it, let's just delete it again

sudo yum -y remove copy-jdk-configs-3.3-11.el7_9.noarch

insert image description here

It can be found here that there is no such thing, but I took a look, it seems to be a little bit unclean

insert image description here

So I reloaded that environment variable, it should be just because there is no reloading problem

insert image description here

Fortunately, I found that I couldn't find it after reloading, which shows that there is actually no big problem with my operation.

Here I downloaded it again and then deleted it again, so the final statement becomes the following. In fact, there are not too many other residuals in the result seen later. The key point is that please remember to reload the environment variable at the end, even after restarting Machines are also possible, but it is still a good habit to develop.

sudo yum -y remove java-11-openjdk-headless-11.0.18.0.10-1.el7_9.x86_64 remove copy-jdk-configs-3.3-11.el7_9.noarch

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_68192925/article/details/129600087