[Linux] Change jdk version

I. Introduction

After we create the server, we need to deploy the project to the server. At this time, we should consider whether the jdk version number of the server is consistent with the jdk version number used in the project. If not, you need to change the jdk version number in the server

2. View the jdk version number

1. The version number in the project (pom.xml)

It can be seen that the jdk version used in the project is 11
insert image description here

2. The version number in the server

#查看jdk版本号
java -version

insert image description here
The jdk version in the project is inconsistent with the jdk version in the server, so we need to replace the jdk version in the server

3. Replace the jdk version

1. Create a java folder

#创建Java文件夹
mkdir /usr/local/java

#切换至/usr/local路径下,进行查看创建好的java文件夹
cd /usr/local
ls

insert image description here

2. Download and decompress the JDK installation package

①. Download the jdk installation package

insert image description here

Upload the downloaded jdk installation package to the server, usually under the /tmp/ path

insert image description here

②. Move to the created /usr/local/java path

#移动到创建好的/usr/local/java路径下
mv jdk-11.0.19_linux-x64_bin.tar.gz /usr/local/java

#切换路径
cd /usr/local/java

insert image description here

③. Unzip the jdk installation package

#对jdk进行解压
tar -zxvf jdk-11.0.19_linux-x64_bin.tar.gz

#查看解压好的jdk
ls

insert image description here

4. Delete the original jdk version

1. Delete the original jdk version

rm -f /usr/bin/java 

rm -f /usr/bin/javac

rm -f /etc/alternatives/java

rm -f /etc/alternatives/javac

insert image description here

2. Enter the profile file

#切换到根目录
cd ~

#进入到/etc路径下
cd /etc/	

#编辑profile文件
vi profile

insert image description here

3. Add environment variables

After entering the profile file, enter i to edit, put the following environment variables at the bottom, then press ESC to exit editing, enter: wq to save and exit

# jdk environment
#JAVA_HOME是安装jdk的路径
JAVA_HOME=/usr/local/java/jdk-11.0.19
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

insert image description here

4. Reload the profile file

#重新加载profile文件
source /etc/profile

insert image description here

5. Check the jdk version

#查看jdk版本
java -version

insert image description here
At this time, we can see that the jdk version in the server has been changed to 11.

6. After changing the jdk version, the linux command fails (stepping on the pit record)

1. Reasons for command failure

The environment variables I configured in the /etc/profile file are as follows:
insert image description here
After I changed the jdk version at the beginning, it was still valid to enter other commands. When I restart the server or reopen a server window and enter the command again, the command fails and prompts: command not found
insert image description here

2. Temporary solution

Execute the following command to enter the linux command, but it is only temporarily valid.

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin

insert image description here

3. The final solution

I found other methods on the Internet and found that the contents of the configuration environment variables provided on the Internet are different from the contents of the environment variables I configured:

In the line PATH= JAVAHOME / bin, JAVAHOME / bin should be placed before PATH, make sure to search the bin directory of JDK first. The correct way to write is: PATH = JAVAHOME / bin : JAVA_HOME/bin In this line, JAVA_HOME/bin should be placed before PATH, make sure to search the bin directory of JDK first. The correct way to write is: PATH=JAVA_HOME/bin:JAVAHIn the OME / bin line, the J A V AHOME / bin is placed before P A T H , make sure to search the bin directory of J DK first . The correct way to write it is: P A T H=JAVAHOMR / bin:PATH

And the configuration environment variable I wrote did not add: $PATH, so it caused the command to fail.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45490023/article/details/131991598