文档编写时间:2021/07/14
zhanghe@test:~$ whoami zhanghe zhanghe@test:~$ date Wed Jul 14 12:45:21 UTC 2021 zhanghe@test:~$ cat /proc/version Linux version 4.15.0-112-generic (buildd@lcy01-amd64-027) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 zhanghe@test:~$ uname -a Linux test 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
下载东西需要注册账号,这一点有点上火,下载完之后文件名是jdk-8u291-linux-x64.tar.gz
sudo su root@test:/home/zhanghe# tar -zxvf jdk-8u291-linux-x64.tar.gz root@test:/home/zhanghe# mv jdk1.8.0_291 /usr/local/jdk1.8 root@test:/home/zhanghe# ls /usr/local/jdk1.8 bin COPYRIGHT include javafx-src.zip jmc.txt jre legal lib LICENSE man README.html release src.zip THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt
vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=.:${JAVA_HOME}/bin:$PATH root@test:/home/zhanghe# source /etc/profile root@test:/home/zhanghe# java -version java version "1.8.0_291" Java(TM) SE Runtime Environment (build 1.8.0_291-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
这个安装太简单,但是后续会遇到一些问题,那我现在从root退出到普通用户以后,发现jdk就不能用了,哪怕再进入root也不能用了,如下所示,这是怎么回事?其实重启就好了,但是不重启怎么办呢?
root@test:/home/zhanghe# java -version Command 'java' not found, but can be installed with: apt install default-jre apt install openjdk-11-jre-headless apt install openjdk-8-jre-headless root@test:/home/zhanghe# source /etc/profile root@test:/home/zhanghe# java -version java version "1.8.0_291" Java(TM) SE Runtime Environment (build 1.8.0_291-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode) root@test:/home/zhanghe# exit exit zhanghe@test:~$ sudo su root@test:/home/zhanghe# java -version Command 'java' not found, but can be installed with: apt install default-jre apt install openjdk-11-jre-headless apt install openjdk-8-jre-headless
解决方法,我们不要用sudo su来切换用户,而要用sudo su - 来切换,这样就会切换用户时读取/etc/profile文件,如下所示:
zhanghe@test:~$ sudo su root@test:/home/zhanghe# exit exit zhanghe@test:~$ sudo su - root@test:~# java -version java version "1.8.0_291" Java(TM) SE Runtime Environment (build 1.8.0_291-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)