How to Install Java on Windows, Ubuntu, and macOS

Reading Time: 6 minutes

Java is one of the world's most popular and widely used languages, used to develop everything from mobile apps and enterprise software to scientific computing. One of the most significant advantages of Java is that it can run almost anywhere after being compiled. This article shows how to install Java on Windows, Ubuntu, and macOS. 

Java-specific Definitions

Before starting the Java install, here are a few helpful definitions.

  • JDK (Java Development Kit) is a software development environment allowing developers to code and run Java programs and applications. Java developers can use it on Windows, macOS, and Linux. Installing JDK will install both JRE and JVM.
  • JRE (Java Runtime Environment) is needed to run Java applications. However, it can’t be used for development purposes and is installed along with JDK.
  • JVM (Java Virtual Machine) is an essential piece of JDK and JRE. Java compiler produces code for a virtual machine called JVM, which interprets Java bytecode into machine language on another system.

Requirements 

  • Windows, macOS Monterey, or Ubuntu 20.04 operating system
  • Admin or root-level access.
  • Internet connection
  • Some familiarity with command line or terminal

How to Install Java on Windows 11

Step 1: How to Download Java for Windows

Visit the Java download page to download the x64 Installer version of Java.

Note:
Those looking for the long-term support (LTS) version of JDK should install Java 17 as it is unavailable for Java 18.

Step 2: Run the Executable

Run the downloaded executable file from the browser or the file manager.

Step 3: Complete the Java Install

Take note of the installation path during this step. After this, you must set the JAVA_HOME variable to point to this path. This variable shows other software where Java is installed.

Click Next, follow the default installer prompts, and complete the installation.

Step 4: Set the Environment Variables

To set environment variables, use Windows Search to find and open Advanced System Settings.

Step 5: Configure Environment Variables

Once you are in Advanced System Settings, click Environment Variables.

Step 6: Create a New System variable

Under System variables, select Path and click New.

Step 7: Set the Java Installation Path

The New System Variable window appears. For the Variable name, input JAVA_HOME. For the Variable value, input the installation path noted in step 3. Click OK on this screen and the System variables screen to complete the setup.

Step 8: Open Windows Terminal

To open the Windows terminal, press the Windows key + X. A small window will pop up. Next, click Windows Terminal. You may need to run the terminal as an administrator depending on your user permissions.

Step 9:  Test the JAVA_HOME Variable

In the terminal, run the following command.

echo %JAVA_HOME%

The command returns the Java installation path.

Step 10: Check the Java Install Version

To check the Java install version, run either of the following commands.

java -version
javac -version

Both commands return the version.

Install Java on macOS Monterey

Step 1: How to Download Java for macOS Monterey

Visit the Java download page and download the Arm 64 DMG Installer if running the M1 CPU chip or x64 DMG on Intel-based CPUs.

Step 2: Open and Run the .dmg File

Open the .dmg file we just downloaded. Run the JDK 17.0.3.1 package.

Step 3: Complete the Java Install

Complete the installation process based on your requirements. The system prompts you to enter your root password to continue the installation. Once entered, click Install Software.

Step 4: Confirm the Java Installation

Run the following command from the terminal to check if Java is installed correctly.

java -version

The output shows the Java version information.

Step 5: Configure Environment Variables

To set up Environment Variables in macOS, place export JAVA_HOME=$(/usr/libexec/java_home) into the ~/.zshenv directory with the following sequence of commands.

% vi ~/.zshenv
% cat ~/.zshenv
export JAVA_HOME=$(/usr/libexec/java_home)

Following the execution of the commands, the system takes you to the standard prompt.

Apply the changes with the following command.

source ~/.zshenv

The system returns you to the prompt where you will run the next command to confirm the Environment Variable has been set. Once executed, the system returns you to the prompt, completing the installation of JavaJDK on macOS Monterey.

echo $JAVA_HOME

How to Install Java on Ubuntu 20.04

Step 1: Confirm an Existing Java Install

Ubuntu 20.04 comes with Open JDK 11 by default. Use the following command to confirm Java is already installed.

java -version

If Java is installed, the terminal outputs the version information. If not, the output states the java command is not found.

Step 2: Update Packages and Install Dependencies

This tutorial installs JavaJDK 17 on Ubuntu 20.04. Update the package index and any existing packages on the system. These commands must run with root privileges.

sudo apt update && sudo apt upgrade-y

Run the following command to install the required dependencies before installing JavaJDK.

sudo apt install libc6-i386 libc6-x32 libxi6 libxtst6 -y

Step 3: Download and Install the Java Package

Use the following commands to download and install the .deb package from the Oracle website. First, download the file.

sudo wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb

Next, install the package.

sudo dpkg -i jdk-17_linux-x64_bin.deb

Step 4: Set the Java Install Location

Run the following commands to show the system where Java is installed.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17/bin/javac 1

Step 5: Set Up Environment Variables

To set up the Environment Variables in Ubuntu 20.04, add JAVA_HOME="/usr/lib/jvm/jdk-17/" to the last line in the /etc/environment file. This tutorial accomplishes this using the Vim text editor. Use the following command to open the file in the terminal.

vim /etc/environment

At the prompt, press the i key and then Enter to enter insert mode in Vim. Once you have inserted JAVA_HOME="/usr/lib/jvm/jdk-17/" into the last line of the file, use the :wq command to save and quit the file.

Apply the file changes with the source command.

source /etc/environment

To test that the Environment Variable is set correctly, use the following command.

echo $JAVA_HOME

The system returns the appropriate location if done properly.

/usr/lib/jvm/jdk-17/

Run the java -version command again to confirm Java is installed. The system outputs the appropriate version of Java.

java version "17.0.3.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment (build 17.0.3.1+2-LTS-6)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3.1+2-LTS-6, mixed mode, sharing)

Wrapping Up

Developers using Java tools, including JDK, JRE, and JVM, create many of the applications we use today. Knowing how to install Java on all three major platforms helps get your operating system and integrated development environment (IDE) of choice ready.

Once you are ready to host your Java application, look no further than Liquid Web. Our VPS Hosting, Cloud Dedicated Servers, and Dedicated Servers provide the perfect hosting environment for your project. Contact us today to choose the right fit.

About the Author: Freddy Reese

Freddy works in the Liquid Web Managed Hosting Support team with a strong passion for all things related to Linux administration, cybersecurity, and aviation. In his free time, he likes to keep up with the latest news on topics ranging from fusion to space technologies. His hobbies include automating all kinds of stuff using Arduino/Raspberry Pi, learning and flying around in flight simulators, playing with his dog Chupko, swimming at nearby beaches, and staying physically and mentally healthy by going to the gym.

Latest Articles

Install Multiple PHP Versions Using EasyApache 4

Read Article

Install Multiple PHP Versions Using EasyApache 4

Read Article

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article