How to install Java OpenJDK 17 & Maven to Ubuntu
Published on

How to install Java OpenJDK 17 & Maven to Ubuntu

Authors

Install Java OpenJDK 17 to Ubuntu

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.

Here are the steps to install Java OpenJDK 17 to Ubuntu

sudo apt install openjdk-17-jdk
sudo update-alternatives --config java

Let me explain the above commands.

The first command, sudo apt install openjdk-17-jdk, uses the Advanced Package Tool (apt), a package management system used by Ubuntu and other Debian-based Linux distributions. The sudo prefix runs the command with superuser privileges, which are necessary for installing software. openjdk-17-jdk is the package name for OpenJDK 17 Java Development Kit (JDK). The JDK includes tools needed to develop applications and services written in the Java programming language. Make sure to accept the installation prompt by typing y and pressing ENTER.

The second command, sudo update-alternatives --config java, updates the symbolic links that let you choose which Java runtime environment (JRE) to use. The command lists the installed JREs and prompts you to select one. Type the number of the JRE you want to use and press ENTER.

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

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/jdk-17-oracle-x64/bin/java       285286400 auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode
  2            /usr/lib/jvm/jdk-17-oracle-x64/bin/java       285286400 manual mode

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

The asterisk (*) indicates the current selection. In the example above, the current selection is JDK 17 from Oracle. The command will update the symbolic links to use the JDK 17 from OpenJDK instead.

We are not done yet. We need to set the JAVA_HOME environment variable to the path of the JDK installation directory. This variable is used by other programs to determine the Java runtime environment to use.

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

Install Maven for building Java projects

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies.

sudo apt install maven

But this will install Maven 3.6.3 which is not the latest version. Sadly we can't install the latest version of Maven using apt. We can install using SDKMAN!.

curl -s https://get.sdkman.io | bash
source ~/.sdkman/bin/sdkman-init.sh
sdk install maven

The first command, curl -s https://get.sdkman.io | bash, uses curl to download the SDKMAN! installation script from https://get.sdkman.io. The -s option tells curl to operate in silent mode, which means it won't output progress information or error messages. The | bash part pipes the downloaded script to bash, which executes the script and installs SDKMAN!.

The second command, source ~/.sdkman/bin/sdkman-init.sh, sources the SDKMAN! initialization script. This script adds SDKMAN! to your PATH environment variable, which lets you run the sdk command from the terminal.

The third command, sdk install maven, uses SDKMAN! to install Maven.

sdk use maven 3.9.6
echo export MAVEN_HOME=$MAVEN_HOME >> ~/.bashrc

The 3.9.6 is just an example. You can use any version you want just run the command sdk use maven <version> to install the required version. You can find the list of available versions by running the command sdk list maven.

The first command, sdk use maven 3.9.6, makes SDKMAN! to use Maven 3.9.6. The second command, echo export MAVEN_HOME=$MAVEN_HOME >> ~/.bashrc, adds the MAVEN_HOME environment variable to the .bashrc file. This variable is used by other programs to determine the Maven installation directory.

Conclusion

In this post, we've seen how to install Java OpenJDK 17 to Ubuntu. We've also seen how to install Maven using SDKMAN!. If you have any questions, please leave a comment below.