【Ubuntu】安装Java 环境和配置环境变量

本文详细介绍了在Ubuntu 18.04系统中如何安装OpenJDK和Oracle JDK,包括更新软件包、安装步骤及环境变量配置。对于Oracle JDK,由于PPA方式已废弃,建议采用手动方式安装,并注意Oracle JDK的最新许可证变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

目录

一. 介绍

二. 安装 Open JDK 版本

三. 安装 Oracle JDK 版本

3.1 PPA 方式(已废弃)

3.2 手工方式

四. 环境变量

五. 参考


 

一. 介绍

Java 有 2 个,一个是openjava,一个是oracle的java,默认系统安装的是openjava,如果你只是普通用户,运行java程序而已,openjava足矣,如果你是搞java开发的,老老实实用sun-java,别跟自己的工作过不去。

安装Java 的最简单方法是使用与Ubuntu一起打包的版本。默认情况下,Ubuntu 18.04包含Open JDK,它是JRE和JDK的开源变体。

 

二. 安装 Open JDK 版本

本地环境配置:

系统:Windows 10 x64

Ubuntu 系统: Windows Subsystem for Linux,

Ubuntu 版本:Ubuntu 18.04 LTS

首先,看下自己本地机器上是否有java,使用如下命令:

java -version

如下图所示: 

要安装Java 先更新一下本地软件包,使用如下命令:

sudo apt update

如下图所示:

执行以下命令安装 OpenJDK:

sudo apt install default-jre

此命令将安装Java运行时环境(JRE)。这将允许您运行几乎所有Java软件。

如下图所示:

此处只需要坐等下载完成,安装完成如下所示:

安装完成后,验证一下安装 Java 的版本:

 

除了JRE之外,您可能还需要Java Development Kit(JDK)才能编译和运行某些特定的基于Java的软件。

如下所示:

要安装JDK,请执行以下命令,该命令还将安装JRE:

sudo apt install default-jdk

安装过程如下所示:

 

安装完成如下所示:

安装完成后,验证一下安装 JDK 的版本:

 

此时,看下默认安装的JRE和JDK 目录:

 

三. 安装 Oracle JDK 版本

如果要安装Oracle JDK(Oracle分发的正式版),则需要为要使用的版本添加新的软件包存储库

3.1 PPA 方式(已废弃)

要安装Java 8(最新的LTS版本),首先要添加其软件包存储库:

# 添加源
sudo add-apt-repository ppa:webupd8team/java


# 列举源
sudo apt-key list


# 删除源
sudo apt-key del <keyid>
sudo add-apt-repository -r ppa:webupd8team/java

命令解释:

$ sudo add-apt-repository --help
Usage: add-apt-repository <sourceline>

add-apt-repository is a script for adding apt sources.list entries.
It can be used to add any repository and also provides a shorthand
syntax for adding a Launchpad PPA (Personal Package Archive)
repository.

<sourceline> - The apt repository source line to add. This is one of:
  a complete apt line in quotes,
  a repo url and areas in quotes (areas defaults to 'main')
  a PPA shortcut.
  a distro component

  Examples:
    apt-add-repository 'deb http://myserver/path/to/repo stable myrepo'
    apt-add-repository 'http://myserver/path/to/repo myrepo'
    apt-add-repository 'https://packages.medibuntu.org free non-free'
    apt-add-repository http://extras.ubuntu.com/ubuntu
    apt-add-repository ppa:user/repository
    apt-add-repository ppa:user/distro/repository
    apt-add-repository multiverse

If --remove is given the tool will remove the given sourceline from your
sources.list


Options:
  -h, --help            show this help message and exit
  -m, --massive-debug   Print a lot of debug information to the command line
  -r, --remove          remove repository from sources.list.d directory
  -s, --enable-source   Allow downloading of the source packages from the
                        repository
  -y, --yes             Assume yes to all queries
  -n, --no-update       Do not update package cache after adding
  -u, --update          Update package cache after adding (legacy option)
  -k KEYSERVER, --keyserver=KEYSERVER
                        Legacy option, unused.

 

点击ENTER,同意继续,有如下信息, 此处信息比较重要,解释了如下安装为何会错误,见如下翻译

 The Oracle JDK License has changed for releases starting April 16, 2019.

The new Oracle Technology Network License Agreement for Oracle Java SE is substantially different from prior Oracle JDK licenses. The new license permits certain uses, such as personal use and development use, at no cost -- but other uses authorized under prior Oracle JDK licenses may no longer be available. Please review the terms carefully before downloading and using this product. An FAQ is available here: https://www.oracle.com/technetwork/java/javase/overview/oracle-jdk-faqs.html

Oracle Java downloads now require logging in to an Oracle account to download Java updates, like the latest Oracle Java 8u211 / Java SE 8u212. Because of this I cannot update the PPA with the latest Java (and the old links were broken by Oracle).

For this reason, THIS PPA IS DISCONTINUED.

For Oracle Java 11, see a different PPA -> https://www.linuxuprising.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html

That same PPA also has Oracle Java 13 -> https://www.linuxuprising.com/2019/09/install-oracle-java-13-on-ubuntu-linux.html

and Oracle Java 14 ->
https://www.linuxuprising.com/2020/03/how-to-install-oracle-java-14-jdk14-on.html

Old description:

Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA.

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html
The PPA supports Ubuntu 18.10, 18.04, 16.04, 14.04 and 12.04.

More info (and Ubuntu installation instructions):
- http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease [15.4 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Fetched 257 kB in 1min 11s (3636 B/s)
Reading package lists... Done

电脑翻译如下:

从2019年4月16日开始,Oracle JDK许可证已更改。



针对oraclejavase的新的Oracle技术网络许可协议与以前的oraclejdk许可有很大的不同。新的许可证允许某些用途,如个人使用和开发使用,不需要任何费用,但是在以前的OracleJDK许可证下授权的其他使用可能不再可用。在下载和使用本产品之前,请仔细阅读条款。此处提供常见问题解答:https://www.oracle.com/technetwork/java/javase/overview/oracle-jdk-faqs.html



oraclejava下载现在需要登录到Oracle帐户来下载Java更新,比如最新的oraclejava8u211/javase8u212。因此,我不能用最新的Java更新PPA(旧的链接被Oracle破坏了)。



因此,本PPA 协议终止。



对于Oracle Java 11,请参阅不同的PPA->https://www.linuxingup.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html



同样的PPA也有Oracle Java 13->https://www.linuxingup.com/2019/09/install-oracle-java-13-on-ubuntu-linux.html



和Oracle Java 14->

https://www.linuxingup.com/2020/03/how-to-install-oracle-java-14-jdk14-on.html



旧描述:



Oracle Java(JDK)安装程序(自动下载并安装Oracle JDK8)。在这个PPA中没有实际的Java文件。



重要信息->为什么Oracle Java 7和6安装程序不再工作:http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html



更新:Oracle Java 9已过期:http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html

PPA支持Ubuntu 18.10、18.04、16.04、14.04和12.04。



更多信息(和Ubuntu安装说明):

- http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html



Debian安装说明:

-Oracle Java 8:http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

更多信息:https://launchpad.net/~webupd8team/+archive/ubuntu/java

按[ENTER]继续,或按Ctrl-c取消添加。





Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease [15.4 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Fetched 257 kB in 1min 11s (3636 B/s)
Reading package lists... Done

然后,添加软件库之后,更新安装包列表:

sudo apt update

如下所示:

使用如下命令,安装:

sudo apt install oracle-java8-installer

然后出现如下情况:

 

表达啥意思呢 ?

Oracle Java下载现在需要登录到Oracle帐户才能下载Java更新,例如最新的Oracle Java 8u211 / Java SE 8u212。因此,我无法使用最新的Java更新PPA(并且旧链接已被Oracle破坏)。

因此,此PPA已终止;

因此,需要自己手工执行;

 

 

 

3.2 手工方式

重要的Oracle JDK许可证更新

自2019年4月16日起,Oracle JDK许可证的发行版已更改。

针对Oracle Java SE 的新Oracle技术网络许可协议与先前的Oracle JDK许可有很大不同。新的许可证允许免费使用某些用途,例如个人用途和开发用途-但根据先前的Oracle JDK许可证授权的其他用途可能不再可用。在下载和使用此产品之前,请仔细阅读条款。可在此处获得常见问题解答。

可以通过低成本的Java SE订阅获得商业许可和支持。

Oracle还通过jdk.java.net在开源GPL许可下提供了最新的OpenJDK版本。

 

目前oracle 官网无法登陆下载,后续更新...

 

四. 环境变量

许多使用Java编写的程序使用JAVA_HOME 环境变量来确定Java安装位置。

要设置此环境变量,请先确定Java 的默认安装位置,由于我还没有配置,输入如下命令:

sudo update-alternatives --config java

如下所示:

然后配置JAVA_HOME 环境变量:

vim .profile

修改为如下语句:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
export PATH="$JAVA_HOME/bin:$PATH"

如下图所示:

 

然后使用如下命令,使得上述配置文件生效:

source .profile

使用如下命令,验证JAVA_HOME 配置:

 

五. 参考

  1. https://wiki.ubuntu.org.cn/Wiki%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95
  2. https://wiki.ubuntu.org.cn/Java
  3. https://wiki.ubuntu.org.cn/Sun-java6
  4. https://wiki.ubuntu.org.cn/Java%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE
  5. https://docs.microsoft.com/zh-cn/archive/blogs/wsl/
  6. https://launchpad.net/~webupd8team/+archive/ubuntu/java
  7. https://launchpad.net/~linuxuprising/+archive/ubuntu/java/+packages
  8. https://www.linuxingup.com/2020/03/how-to-install-oracle-java-14-jdk14-on.html
  9. https://www.linuxingup.com/2019/09/install-oracle-java-13-on-ubuntu-linux.html
  10. https://www.linuxingup.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html
  11. http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html
  12. http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
  13. http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
  14. http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html
  15. https://stackoverflow.com/questions/55920389/e-package-oracle-java8-installer-has-no-installation-candidate
  16. https://stackoverflow.com/questions/51010788/unable-to-install-java9-on-ubuntu
  17. https://www.oracle.com/java/technologies/javase-downloads.html
  18. https://www.oracle.com/cn/java/technologies/javase-downloads.html
  19. https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html
  20. https://www.java.com/zh_CN/download/help/path.xml
  21. https://cloud.tencent.com/developer/article/1162527
  22. https://www.zhihu.com/question/292463455/answer/644015823

 

(完)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值