How to Install Java on Mac – Adam the Automator

Not sure how to install Java on Mac? As a developer, having different programming languages, such as Java, installed on your computer is crucial. Java, one of the most popular programming languages, allows you to develop web, mobile, and desktop applications.

In this tutorial, you will learn how to install Java on a Mac in many ways and create a simple project to start your journey to Java.

Read on to expand your repertoire of programming languages!

Prerequisite

This tutorial consists of a hands-on demonstration. To follow, make sure you have the following

: A Mac computer: This tutorial uses

Big Sur, but any Mac running macOS 11.0 or higher

will work. Homebrew installed. Related:How to Install

Homebrew

on macOS A Code Editor

: This tutorial uses Visual Studio Code (VS Code), but any code editor will work.

Related: How to Install Visual Studio Code on Mac

Related:

How to Install Visual Studio Code

on Mac Using Homebrew to install Java on Mac One of the many ways you can

install Java on Mac

is by using Homebrew, a package manager for macOS. Homebrew allows you to quickly install various programs, such as Java, on your Mac with a few commands.

Homebrew is the right package manager for you if you’re more of a command-line person. And in this tutorial, you will install the latest and oldest/specific versions of Java on Mac.

Installing the latest version

Installing multiple versions of Java on your Mac works without any conflict. But the latest version is always recommended as it contains the latest security updates.

To install the latest version of Java on your Mac:

1. Run the following brew update command to update Homebrew.

2. Then run the following command to find the available java formula. Homebrew uses formulas to manage packages and applications.

You can see below that there are many versions of Java available. The java formula is the alias of the latest version.

3. Run the following command to get detailed information (information) about the java formula.

As you can see below, at this time of writing, the latest version of Java is 19, which is not yet installed.

4. Now, run the following brew install command to install java on your Mac.

5. Once installed, run the following ln command to create a symbolic link (symbolic link) pointing to the installed Java version.

In the following command, the following paths are as follows:

/usr/local/opt/openjdk/libexec/openjdk.jdk – The created symbolic link. /Library/Java/

  • JavaVirtualMachines/openjdk.jdk – The path to which the symbolic link points.

Creating a symbolic link is necessary because some applications only work properly if they can find a Java installation in the standard location

.

Enter your password when prompted to complete the process

.

6. Finally, run the following command to print the installed Java version.

The following output confirms that you have successfully installed the latest version of Java on your Mac, where:

  • OpenJDK is an open source implementation of the Java platform
  • .

  • JDK is the Java Development Kit, a set of tools for developing Java applications.

congratulations! You have successfully installed the latest version of Java on your Mac.

Installing an older or specific

version

The latest version of your Java installation should be fine and all. But “version compatibility” is one thing when building or developing programs. The good news is that you can install older or specific versions of Java along with other versions.

To install an older or specific version of Java, you must first find the available ones:

1. Run the following command to search the openjdk formula for all available Java versions. This formula usually contains much older versions of Java than the java formula.

Choose one of the versions available below ([email protected], 11, and 17) that you want to install.

💡 [email protected] is the latest LTS release, and some apps may not yet support it.

2. Then run the following command to install a specific version of Java, replacing <java_version> with the one you prefer.

As you can see below, the choice of this tutorial is the [email protected] version as [email protected] is too old and no longer recommended.

3. After the installation is complete, run the following command, which does not provide output but creates a symbolic link.

4. Finally, run the exact java command below to check the version of your Java installation.

Installing Java

via DMG

package

If you fancy a GUI instead of a command-line environment, you want to try installing Java with a DMG package (a disk image file). A DMG package acts as a virtual optical disc that contains compressed software installation files and other data such as boot images.

Installing Java via a DMG package is a bit more manual. But hey, clicking on a basic installation wizard wouldn’t hurt.

1. Open your favorite web browser and navigate to Oracle’s official Java download page.

2. Then click the DMG

link for the version of JDK you want to install to download your DMG package to your download directory. The tutorial goes with Java 19, the latest version at the time of writing.

3. Once downloaded, double-click the DMG file to launch the installer.

4. On the Getting Started tab, click Continue to confirm the Welcome page.

5. Now, click on Install in the installation process.

6. Enter your password when prompted and click Install Software to authorize the installation.

7. Once installed, click Close to close the installation wizard.

And that’s it! You have now successfully installed Java on your Mac via the DMG package.

8. Finally, run the following command one last time to check the installed Java version.

Switching

between Java versions

Suppose one project requires Java 11, but another requires Java 19. If so, how do you switch between the two versions of Java? Do not worry! You can work on different versions of Java, switching between each, on your Mac at will.

To switch between Java versions:

1. Run the following command to find out the available versions of Java installed on your computer.

Choose a version you want to switch to, as shown below.

2. Next, run the following command to find the current default version of Java.

As you can see below, Oracle JDK 19 is currently the active or default Java version.

3. Run the following echo command to find the current shell ($SHELL).

Notice in the following output that this walkthrough uses the ZSH shell.

Related: Install Oh My Zsh on Ubuntu for a next-level 4 command line

. Now, run the following command to switch to a different version of Java, replacing <version> with your preferred version.

These commands do the following:

export the path of a specific version of Java to the

  • JAVA_HOME environment variable, which tells Java to use the specified version
  • .

  • Source the profile and apply the changes.

💡 Maybe you’re in a Bash shell. If so, run the source ~/.bashrc command instead to get the profile.

Related:How to get started with Git Bash on Windows

💡 Switching to another version of Java does not provide output, but it will then check if the change is a success

.

5. Finally, run the following commands to check the current version of Java.

The following output confirms that the version change is a success, as the current version is set to OpenJDK 11.

Create your first Java project

on Mac

Successful installation of Java and switching between versions does not indicate whether Java is working correctly. How do I test if Java works on your Mac? You’ll create a basic “hello world” program and use Java to compile and run the program.

To create a Java project:

1. Run the following commands to create a directory (mkdir) for your project (hello-world) and change the working directory (cd) to that directory.

These commands do not provide results, but you will check the project files in the following steps.

2. Next, create a file

called HelloWorld.java in your preferred code editor, add the code below to the file, save the changes, and close the file

.

The following code is a simple Java program that prints the Hello World message to the console

.

3. Run the following javac command, which does not provide results, but compiles your program (HelloWorld.java).

4. Now, run the following command to list (ls) all contents (-la) of the working directory (~/hello-world).

You will see a new file named Hello.class generated, as shown below. This file contains the bytecode of the program.

5. Finally, run the following command to run your program (Hello).

This command calls the Java Runtime Environment to run the program, where the Hello class is loaded and the main method is executed.

The following output confirms that the program works as expected and that the Java installation was successful.

Conclusion

In this tutorial, you have learned many ways to install Java on Mac and create a simple Java project. Java is one of the most popular programming languages in the world. Every nook and corner of the tech industry uses Java in one way or another.

And at this point, you can now build your projects with confidence. Plus, because you can quickly switch between versions, you never have to worry about version compatibility.

With this new knowledge, why not build your own Java application and see how it works?

Contact US