When you install Java, it usually means one of two things: you are installing the Java Runtime Environment (JRE) or the Java Development Kit (JDK). The JRE provides the runtime that allows you to run Java programs, whereas the JDK contains the JRE, as well as other tools, that allow you to develop in Java.
We are going to install the JDK here, but to complicate things further, there are different implementations of the JDK—OpenJDK, Oracle Java, IBM Java—and the one we will be using is the default-jdk APT package, which comes with our Ubuntu installation:
$ sudo apt update $ sudo apt install default-jdk
Next, we need to set a system-wide environment variable so that other programs using Java (for example, Elasticsearch) know where to find it. Run the following command to get a list of Java installations:
$ sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press [enter] to keep the current choice[*], or type selection number:
If you have multiple versions of Java on your machine, you’ll be prompted to select the one you prefer. If you encounter below error:
update-alternatives: command not found
you may try installing below package as per your choice of distribution.
Distribution | Command |
---|---|
OS X | brew install dpkg |
Debian | apt-get install dpkg |
Ubuntu | apt-get install dpkg |
Alpine | apk add dpkg |
Kali Linux | apt-get install dpkg |
CentOS | yum install chkconfig |
Fedora | dnf install chkconfig |
Raspbian | apt-get install dpkg |
Docker | docker run cmd.cat/update-alternatives update-alternatives |
update-alternatives Command Examples
1. Add a symbolic link:
$ sudo update-alternatives --install path/to/symlink command_name path/to/command_binary priority
2. Configure a symbolic link for “java”:
$ sudo update-alternatives --config java
3. Remove a symbolic link:
$ sudo update-alternatives --remove java /opt/java/jdk1.8.0_102/bin/java
4. Display information about a specified command:
$ update-alternatives --display java
5. Display all commands and their current selection:
$ update-alternatives --get-selections