一. Ubuntu 安裝JDK的兩種方式
1. 通過apt安裝.
2. 通過官網下載下傳安裝包安裝.
這裡推薦第1種,因為可以通過 apt-get upgrade 方式友善獲得jdk的更新
二. 通過apt安裝(jdk有很多版本, 這裡介紹兩種: openjdk和oracle的JDK)
1. openjdk
(1) 查找合适的openjdk版本:
apt-cache search openjdk
輸入以上指令後會列出很多版本, 選擇自己需要的即可:
default-jdk-doc - Standard Java or Java compatible Development Kit (documentation)
…(省略部分顯示結果)
openjdk-6-source - OpenJDK Development Kit (JDK) source files
openjdk-7-jre-dcevm - Alternative VM for OpenJDK 7 with enhanced class redefinition
openjdk-7-jre-lib - OpenJDK Java runtime (architecture independent libraries)
openjdk-7-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark
openjdk-8-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-8-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-8-doc - OpenJDK Development Kit (JDK) documentation
openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-8-jre-jamvm - Alternative JVM for OpenJDK, using JamVM
openjdk-8-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark
openjdk-8-source - OpenJDK Development Kit (JDK) source files
uwsgi-app-integration-plugins - plugins for integration of uWSGI and application
uwsgi-plugin-jvm-openjdk-7 - Java plugin for uWSGI (OpenJDK 7)
(2) 安裝
sudo apt-get install openjdk-8-jdk
(3) 配置環境變量, 編輯如下檔案:
vim ~/.bashrc
在最後一行加:
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
(4) 測試jdk是否安裝成功:
java -version
顯示如下結果, 則說明安裝成功:
openjdk version “1.8.0_91”
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~15.10.1-b14)
OpenJDK Server VM (build 25.91-b14, mixed mode)
2. oracle的JDK
(1) 添加ppa
sudo apt install software-properties-common
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
(2) 安裝oracle-java-installer
sudo apt-get install oracle-java8-installer
安裝器會提示你同意 oracle 的服務條款,選擇 ok, 然後選擇yes 即可
(3) 設定系統預設jdk
sudo update-java-alternatives -s java-8-oracle
(4) 如果即安裝了jdk7,又安裝了jdk8,要實作兩者的切換,可以:
jdk8 切換到jdk7
sudo update-java-alternatives -s java-7-oracle
jdk7 切換到jdk8
sudo update-java-alternatives -s java-8-oracle
(5) 測試jdk是否安裝成功:
java -version
三. 直接下載下傳jdk壓縮包方式安裝
1. 官網下載下傳JDK
位址:
http://www.oracle.com/technetwork/articles/javase/index-jsp-138363.html,選擇相應的 .gz包下載下傳
2. 解壓縮,放到指定目錄(以jdk-7u60-linux-x64.gz為例)
建立目錄:
sudo mkdir /usr/lib/jvm
解壓縮到該目錄:
sudo tar -zxvf jdk-7u60-linux-x64.gz -C /usr/lib/jvm
3. 修改環境變量
sudo vim ~/.bashrc
檔案的末尾追加下面内容:
#set oracle jdk environment
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_60 ## 這裡要注意目錄要換成自己解壓的jdk 目錄
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
使環境變量馬上生效:
source ~/.bashrc
4. 設定系統預設jdk版本
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_60/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_60/bin/javac 300
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_60/bin/jar 300
sudo update-alternatives --install /usr/bin/javah javah /usr/lib/jvm/jdk1.7.0_60/bin/javah 300
sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk1.7.0_60/bin/javap 300
然後執行:
sudo update-alternatives --config java
若是初次安裝jdk,會有下面的提示 :
There is only one alternative in link group java (providing /usr/bin/java):
/usr/lib/jvm/jdk1.7.0_60/bin/java
5. 測試jdk
java -version
若有如下結果, 則說明jdk 安裝成功:
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
6. 可能出現的錯誤資訊
bash: ./java: cannot execute binary file
出現這個錯誤的原因可能是在32位的作業系統上安裝了64位的jdk, 檢視你安裝的Ubuntu是32位還是64位系統:
sudo uname -m
i686表示是32位, x86_64表示是64位