Setting Up Java 21 and Maven

So I’ve been putting off this Spring Boot project for ages, and today I finally decided to get my environment ready. Looking at my GitHub repos, you’d probably think I’m a Python developer (and honestly, I have been doing a lot with Python lately), but my day job is actually Java development.

Weirdly enough, I haven’t set up a proper Java environment on this Linux machine yet. I figured I’d document the process since the default apt packages for this stuff are ancient, and I specifically wanted Oracle’s JDK instead of OpenJDK for this project.

Getting Java 21 Installed

First up is Java. I wanted Oracle’s JDK 21 since that’s what I’m using at work. Thankfully, Oracle makes this pretty easy these days - just grab the .deb file and install it:

1
2
3
4
5
6
7
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
sudo apt install ./jdk-21_linux-x64_bin.deb
java --version
---------
java 21.0.4 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

Nice! Java 21 is installed and ready to go. Remember when installing Java used to be a nightmare of environment variables and path settings? Those were the days…

Getting Maven Going

Next up is Maven. I checked the version in the default repositories, and oof - way too old for what I need. Instead, let’s grab the latest directly from Apache:

1
2
3
4
5
sudo apt-get remove maven # first remove any old versions you may have installed
wget https://downloads.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz
tar -xvzf apache-maven-3.9.8-bin.tar.gz
sudo mv apache-maven-3.9.8 /opt/
sudo ln -sfn /opt/apache-maven-3.9.8 /opt/maven

This drops Maven into the /opt directory, but it’s not on my PATH yet. I need to add a couple lines to my .bashrc file:

1
2
export M2_HOME=/opt/maven
export PATH=$M2_HOME/bin:$PATH

Then I’ll reload my profile to make the changes take effect:

1
source ~/.bashrc

Let’s check if everything’s working:

1
2
3
4
5
6
7
mvn -v
---------
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
Maven home: /opt/maven
Java version: 21.0.4, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-21.0.4-oracle-x64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.153.1-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Perfect! Maven is using the right Java version, and I’m finally ready to start coding that project I’ve been thinking about for months. Now I just need to find that Spring Boot tutorial I bookmarked…

…wait, where did I put that link?


↤ Previous Post
Next Post ↦