Setting Up a Java Development Environment on
Mac

Matt Speake
Java Easily
Published in
10 min readSep 14, 2021

In this guide, we’re going to give you the low down on how to set up your machine so that you can develop Java programs. Let’s jump in!

Overview

So, from a high level all you need to do is the following:

1. Download and install the JDK (Java Development Kit) — this is the Java platform itself which contains all the tools you need to be able to write and run Java programs

2. Set up some environment variables — these let other apps know where you installed the JDK to and allow your Mac to ‘see’ the tools in the JDK so you and other programs can actually use them.

3. Download and install an IDE (Integrated Development Environment) — this is the ‘proper application’ you’ll use each day which allows you to type in, edit and run your Java programs.

Whether you’re a highly-experienced Java software engineer working at Google, Facebook or Netflix, or you’re a fresh intern starting out in a bank, these are the tools which all professional Java software engineers have to install once, and use every day to code Java.

Instructions

Downloading and Installing the JDK

Apple give you a very quick and easy way to get started installing Java on your system:

Open up a Terminal session by launching the Terminal app. You can navigate to this within Finder, just go to Applications > Utilities > Terminal. Note also that it’s a good idea to drag this to the Dock at the bottom of the screen too so that you can always access it easily in future.

Once you’re in a Terminal session, type the command java and hit enter:

At this point, a dialog will open which says you need to install the JDK:

  • Click More Info… and you’ll be taken to the downloads page on the Oracle website:

This lists all of the different versions of the JDK which exist since Java was first released up until the very latest release.

  • Now, scroll down to the Java SE 8 section — this is where you can download the 1.8 JDK version which is the best version for beginners to get started with:
  • Next, click the JDK Download link and you’ll be taken to the page which has the different installers available for all different operating systems:

So here you can see there are versions for Windows and Linux amongst others — but for Mac you want the one and only installer which is available called Mac OS x64 (this means the 64-bit version of the JDK which runs on all Mac machines):

  • Next, accept the terms and click the big green button to download the installation package.

At this point you’ll be invited to log in with your Oracle Account.

So if you haven’t signed up for this yet (which you won’t have as you’re reading this for the fist time), then you’ll need to do this before you can download the installer.

It’s completely free to do — you’ll just need a valid email address to create an account.

It’s an unfortunate requirement of Java development that you need to do this, but everyone needs to — so just get this done and you can then download the installer. You’ll never have to sign up again though, I promise! Plus you can access all of Oracle’s Java downloads from then on.

  • Sign in with your Oracle username and password:

and the installation package will start to download:

Once it’s done, you’ll see the installation package:

  • Double click the installation package to start the installation and you’ll be shown the start screen of the installer:
  • Click Continue and then Install at which point you’ll have to put in your username and password to confirm the installation:

Do this and click Install Software and the installer will run and copy across the package files.

  • Click Close once this has finished.

Upon completion the installer will ask if you want to keep the installation file or not. I like to keep install packages around in case I ever need them again in future, to save having to re-download it. If you’re of the same mind, then click Keep to keep it, otherwise you can click Move to Trash.

Once you’ve done this, the JDK has been installed on your system!

Setting up the Environment Variables

Next up we need to set up two important environment variables:

Environment variables allow other programs to find out information that they might need. In our case, these “other programs” are typically utilities we’ll use as Java developers which need to know where the JDK has been installed to.

Step 1: Finding out which version of Mac you have

In order to set up our environment variables, we need to put them in the correct file that our shell reads when it starts up when you open a Terminal session (the shell is the real program that runs inside Terminal when you type in commands at the commandline).

The shell used by Terminal changed recently, so it’s important to understand which version of Mac we have since this determines which version of the shell we’re using:

  • Go to Apple Menu > About this Mac and you’ll see the version of Mac OS X you’re using in the Overview tab of the About pane that appears:

Above you can see it’s 10.15.7 which is Catalina. Note that all the Mac OS X versions have a name — here’s a handy table which shows you which version has which name and, more importantly, uses which shell:

  • From the version of Mac you just discovered you’re using, and the table above, determine which shell you’re running in Terminal sessions. (e.g. I’m running Catalina in the example, and so my shell would be Z Shell).

Step 2: Finding out out where the JDK was installed to

The installer installs the JDK to your system, but it doesn’t tell you where it installed it to. However, there’s a handy command you can use to find this out:

  • Open a Terminal session by launching Terminal (if you don’t have it open from before)
  • Type /usr/libexec/java_home and hit enter - you'll get the directory where the JDK is installed to - this is your "Java Home" environment variable which we'll set next.
  • Highlight this directory by clicking and dragging to highlight the path, then typing CMD+C to copy it to the clipboard:

NOTE: You can take a quick peek into the contents of the folder by doing CMD+N to bring up a new Finder window, typing SHIFT+CMD+G to go to a specific folder, then pasting that directory in there and hitting enter. You'll see the contents of the JDK:

If you ever wondered what the JDK looked like — this is it: it’s a collection of libraries and commandline utilities (mini apps you run inside Terminal — those are inside the binsubdirectory which is what we'll set in the System Path environment variable shortly).

Step 3: Defining JAVA_HOME and adding the JDK tools to the System Path environment variable

So, we now have the path that the JDK was installed to — this is our “Java Home”, we just need to set this as an environment variable and then add the bin subdirectory underneath this to our System Path: this is so that:

  1. other applications (e.g. the IDE we’ll install later in this guide) know where our JDK has been installed to (so it can access the components inside it)
  2. whenever we type commands from the JDK in at the commandline in a Terminal session, our shell knows where to find them to be able to execute them
  • Take the shell you discovered you’re using and note which of these files you’ll need to edit:

NOTE: you can also find out the shell being used by typing in echo $SHELL and hitting enter too:

  • /bin/bash means you're using Bash
  • /bin/zsh means you're using Z Shell
  • Edit the file by typing vi ~/.zshenv for Z Shell or vi ~/.bashrc for Bash. This will take you in to the vi editor. NOTE: This is a quirky editor to work with, and you have to press the keys exactly as defined here!
  • Press i to enter “insert mode” (this is where you can type into and edit the file):
  • Now type the following two lines, substituting my JDK install directory for yours (noting that you can press CMD+V to paste the JDK installation directory in when setting up the Java Home environment variable):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
export PATH=%JAVA_HOME/bin:$PATH

These two lines set up Java Home and then set the System Path variable to add the tools in the JDK.

  • Next, press Escape and type wq and hit Enter. (This leaves insert mode, enters "command mode" and issues the command to write and save the file).

NOTE: You can check the file has been written by doing something like cat ~/.zshenv afterwards.

  • Finally, quit Terminal completely by right-clicking it’s icon in the Dock and selecting Quit.

You’ve now set up your environment variables!

Step 3: Validate that the environment variables are set up correctly

Now to do a quick test — the moment of truth!

  • Re-launch Terminal by clicking it in the Dock — the shell will now be initialised from the file you just edited, and so the new environment variable values should be set.
  • Now type the following commands to check your setup:

Here’s what that might look like if you’re using Bash for example:

NOTE: all the versions should show the same as the version of the JDK you downloaded — if they do, you’re good to go! Well done! ;)

Downloading and Installing an IDE

Good news! The IDE is the easiest part of the whole process!

NOTE: Even though we do our actual programming inside this application, it was still important to do the other earlier steps though to ensure our development environment was properly configured since the IDE relies on being able to locate the JDK and the tools inside it (e.g. for compiling your Java code (with the javac command, which is the Java compiler) and being able to run it afterwards (with the java command, which is the Java Virtual Machine).

  • Go to the Jetbrains website (Jetbrains is the company which makes the IntelliJ IDEA — this is the IDE we’ll download and install) and click the Download button.
  • Go to click the Other Versions on the left underneath the IDEA logo.

NOTE: We’re going to download an older version — the most recent 2019 version. This may seem strange, but in my opinion it’s easier for beginners. Ironically, Jetbrains added some extra features to try and make it easier for beginners, but I personally think they’re too confusing and therefore the version before they added these changes are better for you, so we’ll download this version.

You’ll be taken to the downloads page:

  • Scroll down to the 2019 versions and click the link 2019.3.5 — macOD (dmg) in the IntelliJ IDEA Community Edition column (this is the free version; the Ultimate version is a paid offering that most organisations use, but we’ll use the free one which has more than enough features!) to download IDEA:
  • Once the install package has downloaded, click it to open it and drag the IDEA logo to the Applications folder to start the installation process:
  • Right-click on the installer once it’s run, and click Eject.
  • At this point IDEA is installed and you should be able to type CMD+SPACE to do a Spotlight search, type idea into the search box and you'll see it appear underneath. Hit Enter to launch it:
  • A dialog will pop up asking if you’re ok to launch it, click Open:

At this point IDEA is launched:

Congratulations — you’ve successfully installed IntelliJ IDEA and can now start creating projects and start coding in Java! ;)

Where Next?

If you enjoyed this or prefer to follow along by video, go check out our Java Beginners Starter Kit to jump start your Java journey and learn Java from scratch!

--

--

Matt Speake
Java Easily

Matt Speake is a principal trainer at Java Easily where you can find him writing and recording about all things Java.