How to install Java and Maven on MacOS, Linux and Windows

Install Java On MacOS

In this post, we'll be focusing on Oracle Java, but you can also download OpenJDK if you prefer (from https://openjdk.org/ - See https://jdk.java.net/archive/ for older versions).

Step 1: Download Java from Oracle

To download Java from Oracle, go to https://www.oracle.com/java/technologies/downloads/. From there, you can select either Java 20 or 17 LTS for MacOS, depending on your preference. You'll also need to choose between ARM64 or x64 architecture.

Step 2: Install Java with the DMG Installer

Once you've downloaded the Java installer, open it and follow the instructions provided. The process is simple and should only take a few minutes to complete.

When the installation is finished, you'll see a message that says "Install Succeeded". At this point, you can select "Move to Bin" to remove the installer from your computer.

Step 3: Verify the Installation

To make sure that Java has been installed correctly on your MacOS device, open a terminal and type in the following command: java --version. If everything has been installed correctly, you should see a message that displays your Java version, build, and other relevant information. For example:

java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

If you prefer to install Java from an archive, there's a slightly different process involved, but it's still a simple one. Follow these steps to download and extract the archive, and set up your environment variables:

Start by downloading the Java archive. Next, open up a Terminal and type in the following command to extract the archive: tar -xvf jdk-17_macos-aarch64_bin.tar.gz. This will create a folder named jdk-17.0.6.jdk.

You can move the newly created folder to your home directory. This will make it easier to access and use Java in the future.

Then, you'll need to set up some environment variables. Start by opening up your .bash_profile file in a Terminal window using the command nano /Users/user/.bash_profile. Replace user with your actual username.

Once you have the file open, append the following lines to the bottom of the file:

export JAVA_HOME=$HOME:/OpenJDK/jdk-17.0.6.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

Save the file by pressing ^+O, and then exit by pressing ^+X.

In order for the changes to take effect, you'll need to reload the .bash_profile file. You can do this by running the following command in your Terminal window: source /home/user/.bash_profile. Again, replace user with your actual username.

To verify that the environment variables have been set up correctly, run the command echo $JAVA_HOME. This should display the path to your Java installation, which should look something like this: /Users/user/OpenJDK/jdk-17.0.6.jdk/Contents/Home.

Install Maven On MacOS

Step 1: Download the Binary Zip Archive

Start by going to the Maven website (https://maven.apache.org/) and clicking on the "Download" button. From there, select "Binary zip archive" to download the latest version of Maven.

Step 2: Extract the Archive

Once you've downloaded the archive, extract it into your preferred directory. This will create a folder named apache-maven-3.9.1.

Step 3: Set Up Environment Variables

To use Maven, you'll need to set up some environment variables. Open up your .bash_profile file in a Terminal window using the command nano /Users/user/.bash_profile. Replace user with your actual username.

Once you have the file open, append the following lines to the bottom of the file:

export M2_HOME="/Users/user/Downloads/apache-maven-3.9.1"
export PATH="${M2_HOME}/bin:${PATH}"

Save the file by pressing ^+O, and then exit by pressing ^+X.

Step 4: Reload the .bash_profile File

In order for the changes to take effect, you'll need to reload the .bash_profile file. You can do this by running the following command in your Terminal window: source /home/user/.bash_profile. Again, replace user with your actual username.

Step 5: Verify the Installation

To verify that the environment variables have been set up correctly, run the command echo $M2_HOME. This should display the path to your Maven installation, which should look something like this: /Users/user/Downloads/apache-maven-3.9.1.

To make sure everything is working properly, run the mvn --version command in your Terminal window. This should display information about your Maven installation, including the version number, Maven home directory, and Java version.

mvn --version
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: /Users/user/Downloads/apache-maven-3.9.1
Java version: 17.0.6, .....

Install Java On Windows

  1. Go to https://www.oracle.com/java/technologies/downloads/ and select Windows.

  2. Download the x64 installer (the .exe file).

  3. Open the .exe installer and follow the instructions.

  4. When done, open a Command Prompt and run java --version. You should see a message indicating the Java version installed on your machine.

Install Maven on Windows

  1. Go to https://maven.apache.org/ and click on Download.

  2. Download the Binary zip archive.

  3. Extract the archive. It will create a folder named apache-maven-3.9.1.

  4. Open the Start menu and search for edit system environment variables. A dialog will pop up. Click on Environment Variables....

  5. Add a new System Variable with:

Variable name: MAVEN_HOME
Variable Value: -> Click on Browse Directory... and select the path to the extracted archive (C:\Users\user\Downloads\apache-maven...)

Click on Ok when done.

  1. Then, select the Path variable -> click on Edit -> click on New

  2. Enter %MAVEN_HOME\bin% and click Ok to save the changes

  3. Open a Command Prompt and run mvn --version. You should see a message indicating the Maven version installed on your machine, along with other relevant information.

Install Java On Linux

  1. Download Java from Oracle, go to https://www.oracle.com/java/technologies/downloads/. From there, you can select either Java 20 or 17 LTS for Linux, depending on your preference. You'll also need to choose between ARM64 or x64 architecture.

  2. If you want to install it from a .deb package, open a terminal, go to the Downloads folder and run sudo dpkg -i jdk-17_linux-x64_bin.deb

2.1. To setup the env variables, open /home/user/.bashrc with nano or other text editor and append:

export PATH=/usr/lib/jvm/jdk-17/bin:$PATH
  1. If you want to setup Java from the archive, open a terminal, go to the Downloads folder and extract it with tar -xvf jdk-17_linux-x64_bin.tar.gz

3.1. Set up some environment variables - append to ~/.bashrc:

export PATH=/home/user/Downloads/jdk-17.0.6/bin:$PATH
  1. Run source ~/.bashrc to reload bashrc in the current terminal

  2. Verify the Installation: java --version

Install Maven On Linux

Here's how you can install Maven on Linux:

  1. Go to https://maven.apache.org/ and click on Download.

  2. Download the Binary zip archive.

  3. Extract the archive. It will create a folder named apache-maven-3.9.1.

  4. Open ~/.bashrc with nano (or other text editor) and add the bin directory to the PATH environment variable:

export PATH=/home/user/Downloads/apache-maven-3.8.6-bin/apache-maven-3.8.6/bin:$PATH
  1. In order for the changes to take effect, you'll need to run source ~/.bashrc

  2. To make sure everything is working properly, run the mvn --version command in your Terminal window