目录
一. 介绍
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 配置:
五. 参考
- https://wiki.ubuntu.org.cn/Wiki%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95
- https://wiki.ubuntu.org.cn/Java
- https://wiki.ubuntu.org.cn/Sun-java6
- https://wiki.ubuntu.org.cn/Java%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE
- https://docs.microsoft.com/zh-cn/archive/blogs/wsl/
- https://launchpad.net/~webupd8team/+archive/ubuntu/java
- https://launchpad.net/~linuxuprising/+archive/ubuntu/java/+packages
- https://www.linuxingup.com/2020/03/how-to-install-oracle-java-14-jdk14-on.html
- https://www.linuxingup.com/2019/09/install-oracle-java-13-on-ubuntu-linux.html
- https://www.linuxingup.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html
- http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html
- http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
- http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html
- https://stackoverflow.com/questions/55920389/e-package-oracle-java8-installer-has-no-installation-candidate
- https://stackoverflow.com/questions/51010788/unable-to-install-java9-on-ubuntu
- https://www.oracle.com/java/technologies/javase-downloads.html
- https://www.oracle.com/cn/java/technologies/javase-downloads.html
- https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html
- https://www.java.com/zh_CN/download/help/path.xml
- https://cloud.tencent.com/developer/article/1162527
- https://www.zhihu.com/question/292463455/answer/644015823
(完)