Centos7 set up the Java environment

Server environment

Ali cloud server ECS Centos7.7

Setup procedure – Method 1

Yum source installation

1. Update the YUM source first and keep pace with The Times

yum update -y

Copy the code

In very bad cases, the following error occurs because the RPM database has a problem

Error: RPMDB open failed #

⭐ noun explanation: RPM (RPM package manager)

The solution

CD /var/lib/rpm # RPMDB directory

Rm -f __db.* # Clear the original RPMDB file

RPM --rebuilddb # rebuild RPM database

# yum clean all # yum clean all

Copy the code

Perfect bug fix, OJBK

2. Uninstall the built-in OpenJDK and related Java files

rpm -qa | grep java

Copy the code

Command description:

The command explain
rpm Management suite
-qa Query all suites using query mode
grep Look for strings in the file that match the criteria
java Find a file that contains Java strings
#If it exists, delete it to avoid interference. RPM -e --nodeps is used to delete the node

The RPM -e -- nodeps Java - 1.7.0 - its - 1.7.0.111-2.6.7.8. El7. X86_64

Copy the code

Command description:

The command explain
rpm Management suite
-e Deletes the specified suite
–nodeps Do not verify the correlation of kit files

To use the RPM – qa | grep Java view, if haven’t remove clean, using yum command to remove

#Uninstall all openJDK related file inputs

Yum - remove Java - 1.7.0 - its * y

Copy the code

3. Install the JDK

  • View the JDK software package list
yum search java | grep -i --color jdk

Copy the code
MUnfxS.png
  • Select version installation
Yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

#Or run the following command to install all files for JDk1.8.0

Yum install Java - y - 1.8.0 comes with - its *

Copy the code
  • Check whether the installation is successful
java -version

Copy the code

4. Configure environment variables

  • The default JDK installation path is /usr/lib/jvm
  • Add environment variables to /etc/profile
#Open the file with the VIm compiler

vim /etc/profile

-------------------------------------------

#JAVA_HOME must be the JDK installation directory !!!!

JAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its 1.8.0.181-3. Bl3. El7_5. X86_64

PATH=$PATH:$JAVA_HOME/bin  

CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  

export JAVA_HOME  CLASSPATH  PATH 

-------------------------------------------

#Force save close

! wq

-------------------------------------------

#Make the configuration file take effect

source  /etc/profile

Copy the code
  • Use the following command to view JDK variables
 echo $JAVA_HOME

 echo $PATH

 echo $CLASSPATH

Copy the code

Setup procedure – Method two

Decompress the tar package

1. Download the JDK tar package you need

  • You can download it from oracle’s official website and click on me to go to 🔗

    MUX7vT.png
  • To download the software package on this dog server, click me to download 📥

2. Upload the tar file to the server and decompress the file

You can use FTP to drag it to the server or go to the directory and run the wget command to download it.

  • Methods a
#Use Filezilla to drag the directory /usr/local/java

#Decompress using commands

 tar -zxvf jdk-8u151-linux-x64.tar.gz

Copy the code
  • Method 2
Use the wget command to download

 cd /usr/local/java

Wget HTTP: / / http://118.190.36.92/software/jdk-8u151-linux-x64.tar.gz

 #Decompress using commands

 tar -zxvf jdk-8u151-linux-x64.tar.gz

Copy the code

3. Configure environment variables

#Open the file with the Vim compiler and add the following at the end of the file

vim  /etc/profile

Copy the code
#JAVA_HOME must be the JDK installation directory !!!!## Change the JDK directory you unzipped

Export JAVA_HOME = / usr/local/Java/jdk1.8.0 _151

export JRE_HOME=${JAVA_HOME}/jre  

export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  

export PATH=${JAVA_HOME}/bin:$PATH  

Copy the code
#Force save close

! wq



#
Update the configuration file to take effect

source /etc/profile

Copy the code
  • Check whether the installation is successful
#Viewing the JDK Version

java -version 

Java version "1.8.0 comes with _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)

Copy the code