How To Install Java with Apt-Get on Ubuntu 16.04


Introduction

Java and the JVM (Java's virtual machine) are widely used and required for many kinds of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.

Prerequisites

To follow this tutorial, you will need:

Installing the Default JRE/JDK

The easiest option for installing Java is using the version packaged with Ubuntu. Specifically, this will install OpenJDK 8, the latest and recommended version.

First, update the package index.


 
 
  • sudo apt-get update

Next, install Java. Specifically, this command will install the Java Runtime Environment (JRE).


 
 
  • sudo apt-get install default-jre

There is another default Java installation called the JDK (Java Development Kit). The JDK is usually only needed if you are going to compile Java programs or if the software that will use Java specifically requires it.

The JDK does contain the JRE, so there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

You can install the JDK with the following command:


 
 
  • sudo apt-get install default-jdk

Installing the Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you will need to follow a few more steps. If you need Java 6 or 7, which are not available in the default Ubuntu 16.04 repositories (not recommended), this installation method is also available.

First, add Oracle's PPA, then update your package repository.


 
 
  • sudo add-apt-repository ppa:webupd8team/java
  • sudo apt-get update

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 6 or 7

These are very old versions of Java which reached end of life in February 2013 and April 2015 respectively. It's not recommended to use them, but they might still be required for some programs.

To install JDK 6, use the following command:


 
 
  • sudo apt-get install oracle-java6-installer

To install JDK 7, use the following command:


 
 
  • sudo apt-get install oracle-java7-installer

Oracle JDK 8

This is the latest stable version of Java at time of writing, and the recommended version to install. You can do so using the following command:


 
 
  • sudo apt-get install oracle-java8-installer

Oracle JDK 9

This is a developer preview and the general release is scheduled for March 2017. It's not recommended that you use this version because there may still be security issues and bugs. There is more information about Java 9 on the official JDK 9 website.

To install JDK 9, use the following command:


 
 
  • sudo apt-get install oracle-java9-installer

Managing Java

There can be multiple Java installations on one server. You can configure which version is the default for use in the command line by using update-alternatives, which manages which symbolic links are used for different commands.


 
 
  • sudo update-alternatives --config java

The output will look something like the following. In this case, this is what the output will look like with all Java versions mentioned above installed.

Output
There are 5 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-6-oracle/jre/bin/java          1         manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          2         manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/java-8-oracle/jre/bin/java          3         manual mode
  5            /usr/lib/jvm/java-9-oracle/bin/java              4         manual mode

Press <enter> to keep the current choice[*], or type selection number:

You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler (javac), the documentation generator (javadoc), the JAR signing tool (jarsigner), and more. You can use the following command, filling in the command you want to customize.


 
 
  • sudo update-alternatives --config command

Setting the JAVA_HOME Environment Variable

Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location. To set this environment variable, we will first need to find out where Java is installed. You can do this by executing the same command as in the previous section:


 
 
  • sudo update-alternatives --config java

Copy the path from your preferred installation and then open /etc/environment using nano or your favorite text editor.


 
 
  • sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.

/etc/environment
JAVA_HOME="/usr/lib/jvm/java-8-oracle"

Save and exit the file, and reload it.


 
 
  • source /etc/environment

You can now test whether the environment variable has been set by executing the following command:


 
 
  • echo $JAVA_HOME

This will return the path you just set.

Conclusion

You have now installed Java and know how to manage different versions of it. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra, or Jenkins.

原文地址:https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04




Ubuntu 16.04系统上更换国内的APT源,通常我们会选择阿里云、163、清华或者中科大的镜像仓库。以下是安装阿里云源的步骤: 1. **关闭自动更新** (防止新版本包替换已设置的源): ```bash sudo update-alternatives --config apt ``` 2. **添加新的APT源列表**: - 阿里云源(ppa): ```bash sudo add-apt-repository ppa:mirrorservice/mirrors.list-ubuntu ``` - 或者直接复制阿里云的国内镜像地址到 `/etc/apt/sources.list` 文件: ```bash sudo nano /etc/apt/sources.list ``` 在文件末尾追加: ``` deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse ``` - 对于163源: ```bash sudo add-apt-repository ppa:chenyuanxiang/deb ``` 或者: ``` deb http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse ``` - 清华源: ```bash sudo add-apt-repository ppa:tsuda-lab/docker-archive-trusted-source-ubuntu ``` 或者: ``` deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse ``` - 科大源(需要先安装中科大PPA): ```bash curl -fsSL https://mirrors.ustc.edu.cn/cygwin-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/ustc-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/ustc-archive-keyring.gpg] https://mirrors.ustc.edu.cn/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ustc.list > /dev/null ``` 3. **刷新并更新软件列表**: ```bash sudo apt-get update ``` 4. **确认新源是否有效**: 查看当前使用的源: ```bash apt-cache policy <package-name> ``` 如果源正常,会显示来自新服务器的信息。 5. **重新启用自动更新**: ```bash sudo update-alternatives --config apt ``` 注意:不同的源可能会有不同的更新频率和服务质量,建议根据个人地理位置和网络状况选择合适的源。更换源可能导致某些软件包更新较慢或无法获取,所以在切换之前应确保重要软件已经安装完毕,并保存好现有的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值