1. 程式人生 > 其它 >ubuntu 安裝jdk 1.8

ubuntu 安裝jdk 1.8

目錄

ubuntu 安裝jdk 1.8

文件編寫時間: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)