ubuntu 安裝java環境變量,我使用的是jdk-11.0.9
1.解壓安裝包到/usr/local/share/
tar -zxvf 安裝包名 /usr/local/share/
此時在/usr/local/share/會出現名爲jdk-11.0.9的文件夾
2.修改文件jdk-11.0.9文件夾的權限:
chown -R root:root /usr/local/share/jdk-11.0.9
3.配置環境變量:
gedit /etc/environment
在尾行换行添加:
export JAVA_HOME="/usr/local/share/jdk-11.0.9"
export JRE_HOME="/usr/local/share/jdk-11.0.9/jre"
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
4.修改root用戶的環境變量(可选):
gedit /etc/profile
尾行换行添加:
export JAVA_HOME="/usr/local/share/jdk-11.0.9"
export JRE_HOME="/usr/local/share/jdk-11.0.9/jre"
export CLASSPATH="$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$Home/bin
5.修改普通用户的环境变量
gedit ~/.profile
尾行换行添加:
export JAVA_HOME="/usr/local/share/jdk-11.0.9"
export JRE_HOME="/usr/local/share/jdk-11.0.9/jre"
export CLASSPATH="$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$Home/bin
6.更新变量:
source ~/.profile 或者 source /etc/profile
7.最后检查一下java环境变量,如果显示java的版本证明成功了,如果没有显示请检查前面的步骤:
java -version
java version "11.0.9" 2020-10-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)