How to Install Java on Mac via Terminal

Many applications we frequently use require Java, and if you are a developer who creates Java applications, then you definitely need to be able to download and install Java on your Mac.

While it is possible to download and install it from your desktop, installing Java on your Mac via Terminal is often the preferred method of many who use a command-line interface like Terminal. One of the best methods to install Java on your Mac through Terminal is with Homebrew.

My name is Eric, and as a software engineer and software configuration manager. I have installed many applications, a large portion of them through a command-line interface like Terminal. I have done many Java installs, and I can show you how.

Keep reading below if you want to see the detailed steps of installing Java on Mac via Terminal.

Install Homebrew

Homebrew is a tool that makes it easy to install applications like Java. By typing the following command, you can check if your system already has it installed.

brew –version

It will give you a response showing the version if you already have it installed. If it says something like command not found, you do not have it installed and will need to install it. You can install it using the following command.

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

Once it starts, it will walk you through the installation, and you will only need to answer a few simple questions. You can take a look here for more information on installing Homebrew.

Install Java

Once you install Homebrew, you are now ready to install Java. Before starting the installation, I first recommend checking to see if you already have a version of Java on your Mac. There’s a chance it could have already been installed by another application.

If there is another version, you may want to take note of the version. You could proceed with the new install if there is a more recent version and you wish to upgrade. You can check if you currently have Java installed with the following command.

java -version

If you already have it installed, it will come back with the current version on your machine. You will see a command not found message or something similar if it is not installed.

If Java has already been installed, you can use Homebrew to check the latest available version. If you already have the newest version, you do not need to install/upgrade. Use the command below to see what the latest version available is.

brew info java

This will provide you with details of the latest version available. If, when running the -version command above, you have the same version, then there is no need to do the install. If there is a more current version or you do not have java at all, you can proceed with the steps below.

Step 1: Run the brew install command

Type the command below into Terminal and hit return/enter.

brew install java

Step 2: You will see output from Homebrew scroll by in Terminal.

Step 3: Verify the Installation

You will want to verify the java installation by looking in the directory in which Hombrew was supposed to install it. Use the ls command below to see if it is there.

ls -la /opt/homebrew/Cellar/openjdk

In the output, you should see a folder with the latest version of java. Our example above shows the 18.0.1.1 version, which is the version Homebrew indicated it was going to install.

Step 4: Create a symbolic link

The Homebrew installation of Java is keg-only, meaning that it installs it in Homebrew’s own directory. It does not create any links and does not add java to your path, so at this point, if you type in a java command, your system will not be able to find it. 

To fix this problem, we can create a symbolic link in a directory that is already in your system’s path and is linked to the new java installation. If you look at the output of the java installation, you will see the command you need to run to do this.

So, type in the following command and then hit return/enter.

sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

You will need to enter your password after entering the above command. Use the same password you use to log into your Mac. Note that the account you log in with must be an account with admin permissions.

Step 5: Verify your version

After completing step 4, you should now be able to test the installation by typing in the java -version command. Type in the command below followed by return/enter.

java -version

You should get a response showing the same version as the java version you just installed. In the example above, it would be 18.0.1.1.

What are JRE and JDK?

When working with and installing Java, you may see references to JRE and JDK and wonder what these are. You may understand that they are part of Java but may also wonder what the difference is between them. Here is a really quick explanation.

JRE

JRE stands for Java Runtime Environment. This is the part of Java that actually runs Java applications and applets. It is used to run any java apps you write and java applications you might download.

JDK

JDK stands for Java Development Kit. This is a set of tools that you need to develop applications in Java. It contains the javac compiler, libraries, and other components you need to create a java application that will run on JRE.

FAQs

Below are a few questions I often see when talking about Java installations.

Can I Install Java without Homebrew?

Yes, there are other ways to install Java without Homebrew. You can even install it by downloading it with your web browser and installing it from your desktop, but in my opinion, Homebrew is the easiest way to install it and ensure you get the latest version.

What does keg-only mean?

When Homebrew installs it as keg-only, this means that it installs it into its own Homebrew directory and does not automatically set up the path, create links or set up compiler settings in your shell profile. As with the link we created above, you need to do this on your own.

Why should I use Java?

One of the greatest advantages of Java is that it is platform-independent. This means you can develop apps on your Mac that will run in any environment, such as macOS, Windows, Linux, etc. It is also one of the most popular programming languages and is very easy to learn.

Conclusion

The easiest way to install Java on your Mac via Terminal is to use Homebrew. If you don’t have Homebrew installed, it is straightforward to install. Once you have Homebrew, it takes only a few simple commands to get Java installed and working.

As usual, let me know if you have any questions or comments. I would love to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *