Java and JVM (Java Virtual Machine) are widely used and are required for a variety of software. This article will guide you through the process of installing and managing different versions of Java using apt-get
.
To follow this tutorial, you need:
The easiest way to install Java is to use the version packaged with Ubuntu. Specifically, this will install OpenJDK 8, which is the latest 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 JDK (Java Development Kit). If you want to compile Java programs or use Java software, you usually only need JDK.
JDK does include JRE, so in addition to the larger file size, if you install JDK instead of JRE, there are no disadvantages.
You can install the JDK with the following command:
sudo apt-get install default-jdk
If you want to install Oracle JDK (the official version distributed by Oracle), you need to perform several 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 you want to install, execute one of the following commands:
This is the latest stable version of Java when it was written, and it is also the recommended installation version. You can do this with the following command:
sudo apt-get install oracle-java8-installer
This is a developer preview version, and the general version is scheduled to be released in March 2017. It is not recommended that you use this version because there may still be security issues and bugs.
To install JDK 9, use the following command:
sudo apt-get install oracle-java9-installer
Multiple Java can be installed on a server. You can use update-alternatives
on the command line to configure the default version and manage which symbolic links are used for different commands.
sudo update-alternatives --config java
The output will look like the following. In this case, this is the output of installing all the Java versions mentioned above.
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 select the number you want to use as the default value. This can also be used for other Java commands, such as compiler (javac
), document generator (javadoc
), JAR signing tool (jarsigner
), etc. You can use the following commands to fill in the commands you want to customize.
sudo update-alternatives --config command
Many programs (such as Java servers) use the JAVA_HOME
environment variable to determine where to install Java. To set this environment variable, we 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 the preferred installation and open /etc/environment
with 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 the path you copied yourself.
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Save and exit the file, then reload.
source /etc/environment
You can now test whether the environment variables have been set by executing the following command:
echo $JAVA_HOME
This will return the path you just set.
You have now installed Java and know how to manage different versions of it. You can now install software that runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.
To learn more about using Apt-Get to install Java related tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Install Java with Apt-Get on Ubuntu 16.04"
Recommended Posts