Java Setup on macOS

Using Java requires you to install a JDK (“Java Development Kit”) or JRE (“Java Runtime Environment”). We’ll use a JDK since it can do everything a JRE can, plus more.

Choosing a JDK

As of 2019, there are two major JDKs: OpenJDK (also known as AdoptOpenJDK) and the Oracle JDK. OpenJDK and Oracle JDK provide the same functionality, and are even built from the same code base. They differ only in their packaging and licensing.

OpenJDK is free for use in all situations. Oracle JDK requires a paid-for commercial license when used in a production setting.

Most personal users will probably want to use OpenJDK, since it’s easier to acquire and install and that’s what we’ll be covering here.

Java Installation

First you should check if Java is already installed

java -version

If you see a non-empty output like below then Java is already installed on your machine and you are good to go

openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)

If you don’t see the output like above then you need to install Java on your system

Installing OpenJDK

Installing OpenJDK using Homebrew

brew install openjdk

If you’re curious, you can list all the available JDK versions via:

brew search jdk

There will be a lot of options here! If you are not a serious Java developer, don’t worry about them, and just use the default openjdk.

Once you’ve done that, check if Java is correctly installed by running the java -version command again.

Downloading and installing OpenJDK manually

Open a web browser and go to https://adoptopenjdk.net/. Select “OpenJDK 11 (LTS)” and “HotSpot”. Click the big “Latest release” button and run the installer file that gets downloaded.

Once you’ve done that, check if Java is correctly installed by opening a new Terminal session and running the java -version command again.

SDKMAN

SDKMAN! is a tool for managing parallel versions of multiple software development kits on most Unix-based systems. It provides a convenient command line interface (CLI) and API for installing, switching, removing and listing candidates.

Installation

Open a new terminal and enter:

 curl -s "https://get.sdkman.io" | bash

Follow the instructions on-screen to complete installation. Next, open a new terminal or enter:

source "$HOME/.sdkman/bin/sdkman-init.sh"

Lastly, run the following code snippet to ensure that installation succeeded:

sdk version

If all went well, the version should be displayed. Something like:

sdkman 5.0.0+51

Usage

Installing an SDK

Latest stable

Install the latest stable version of your SDK of choice (say, Java JDK) by running the following command:

sdk install java

You will see something like the following output:

Downloading: java 8u111

In progress...

######################################################################## 100.0%

Installing: java 8u111
Done installing!

Now you will be prompted if you want this version to be set as default.

Do you want java 8u111 to be set as default? (Y/n):
Answering yes (or hitting enter) will ensure that all subsequent shells opened will have this version of the SDK in use by default.
Setting java 8u111 as default.

More commands

  • To install a specific version qualify the version you require:
sdk install scala 2.12.1
  • To remove a version
sdk uninstall scala 2.11.6

Note that removing a local version will not remove the local installation.

  • To list candidates
sdk list

More useful commands and usages can be found at SDKMAN Usage.


Licenses and Attributions


Speak Your Mind