在Ubuntu下配置JDK環境
檢查是否已經安裝了JDK,執行以下命令:
java -v
如果出現以下內容則說明沒有安裝:
程序 'java' 已包含在下列軟件包中: * default-jre * gcj-5-jre-headless * openjdk-8-jre-headless * gcj-4.8-jre-headless * gcj-4.9-jre-headless * openjdk-9-jre-headless 請嘗試:sudo apt install <選定的軟件包>
解壓文件到目標目錄
下載JDK的tar.gz包,然后將其解壓之后,移動到如下路徑:
/usr/lib/java
配置環境變量
修改配置文件:
/etc/profile
修改之前:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi
在末尾增加內容,修改后:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi export JAVA_HOME=/usr/lib/java/jdk1.8.0_151 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH
重新加載配置文件:
source /etc/profile
測試,執行命令查看java版本信息:
java -version
出現結果:
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
此處需要注意,一開始命令寫錯了寫成了 java -v,結果報了個錯:
Unrecognized option: -v
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
此處應該關注的錯誤是第一行的內容,之前沒有注意,結果走了半天彎路。
配置軟鏈接
update-alternatives --install /usr/bin/java java /usr/lib/java/jdk1.8.0_151/bin/java 300
update-alternatives --install /usr/bin/javac javac /usr/lib/java/jdk1.8.0_151/bin/javac 300