贝隆

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

https://developer.aliyun.com/article/704959#:~:text=%E4%B8%80.%20Ubuntu%20%E5%AE%89%E8%A3%85JDK%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F.%201.%20%E9%80%9A%E8%BF%87apt%E5%AE%89%E8%A3%85.%202.%20%E9%80%9A%E8%BF%87%E5%AE%98%E7%BD%91%E4%B8%8B%E8%BD%BD%E5%AE%89%E8%A3%85%E5%8C%85%E5%AE%89%E8%A3%85.%20%E8%BF%99%E9%87%8C%E6%8E%A8%E8%8D%90%E7%AC%AC1%E7%A7%8D%2C%E5%9B%A0%E4%B8%BA%E5%8F%AF%E4%BB%A5%E9%80%9A%E8%BF%87,apt-get%20upgrade%20%E6%96%B9%E5%BC%8F%E6%96%B9%E4%BE%BF%E8%8E%B7%E5%BE%97jdk%E7%9A%84%E5%8D%87%E7%BA%A7.%20%E4%BA%8C.%20%E9%80%9A%E8%BF%87apt%E5%AE%89%E8%A3%85%20%28jdk%E6%9C%89%E5%BE%88%E5%A4%9A%E7%89%88%E6%9C%AC%2C%20%E8%BF%99%E9%87%8C%E4%BB%8B%E7%BB%8D%E4%B8%A4%E7%A7%8D%3A%20openjdk%E5%92%8Coracle%E7%9A%84JDK%29

 

 

一. 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位

posted on 2022-01-19 10:38  贝隆  阅读(2760)  评论(0编辑  收藏  举报