Table of contents
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
2. The version number in the server
#查看jdk版本号
java -version
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
2. Download and decompress the JDK installation package
①. Download the jdk installation package
Upload the downloaded jdk installation package to the server, usually under the /tmp/ path
②. 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
③. Unzip the jdk installation package
#对jdk进行解压
tar -zxvf jdk-11.0.19_linux-x64_bin.tar.gz
#查看解压好的jdk
ls
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
2. Enter the profile file
#切换到根目录
cd ~
#进入到/etc路径下
cd /etc/
#编辑profile文件
vi profile
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
4. Reload the profile file
#重新加载profile文件
source /etc/profile
5. Check the jdk version
#查看jdk版本
java -version
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:
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
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
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.