How to install jdk 17 and maven 3.9.4 in Ubuntu

  devops, linux
# first see what java is installed:
sudo apt list --installed | grep jdk
# and uninstall the old version, ex: sudo apt-get remove openjdk-11-jre-headless

# install jdk 17
sudo apt-get install openjdk-17-jdk-headless

# make sure all looks good:
sudo update-alternatives --config java
sudo update-alternatives --config javac

# update java home to /usr/lib/jvm/java-17-openjdk-amd64
echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc
echo $JAVA_HOME

# get maven
cd ~
wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
sudo tar xzvf apache-maven-3.9.4-bin.tar.gz -C /opt
sudo ln -s /opt/apache-maven-3.9.4 /opt/maven

echo 'export M2_HOME=/opt/maven' >> ~/.bashrc
echo 'export PATH=${M2_HOME}/bin:${PATH}' >> ~/.bashrc
source ~/.bashrc
mvn --version

Example output:

dev@gitlab:~$ javac --version
javac 17.0.7

dev@gitlab:~$ java -version
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment (build 17.0.7+7-Ubuntu-0ubuntu118.04)
OpenJDK 64-Bit Server VM (build 17.0.7+7-Ubuntu-0ubuntu118.04, mixed mode, sharing)

dev@gitlab:~$ mvn --version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /opt/maven
Java version: 17.0.7, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-213-generic", arch: "amd64", family: "unix"