How to Install Java with Apt-get on Ubuntu 16.04

Source: Internet
Author: User

Koen Vlaswinkel  Subscribe Share inHow to Install Java with Apt-get on Ubuntu 16.04PostedApril 1.1m views JAVA ubuntu ubuntu 16.04 Introduction

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

Prerequisites

To follow the tutorial, you'll need:

    • One Ubuntu 16.04 server.

    • A sudo non-root user, which you can set up by following the Ubuntu 16.04 Initial Server Setup Guide.

Installing the Default JRE/JDK

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

First, update the package index.

    • sudo apt-get update

Next, install Java. Specifically, this command would 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 be going to compile Java programs or if the software that would use Java Specificall Y requires it.

The JDK does contain the JRE, so there is no disadvantages if you install the JDK instead of the JRE, except for the Larg ER 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 are the official version distributed by Oracle, you'll need to follow a few More steps.

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 want to install, execute one of the following commands:

Oracle JDK 8

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 was a developer preview and the general release are scheduled for March 2017. It's not recommended so you use this version because there could 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 multiple Java installations on one server. You can configure which version update-alternatives are the default for use with the command line by using, which manages which symbolic Li Nks is used for different commands.

    • sudo update-alternatives--config java

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

Output
  there is 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/jav A 4 manual modepress <enter> to keep the current choice[*], or type selection number:  

You can now choose the number to use as a default. This can also is 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 with 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 the this environment variable, we'll first need to find out where Java is installed. 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 have been set by executing the following command:

    • Echo $JAVA _home

This would return the path you just set.

Conclusion

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

How to Install Java with Apt-get on Ubuntu 16.04

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.