How to install Java on a Mac | Opensource.com

How to install Java on a Mac

macOS users can run the open source release of Java as well as newer frameworks for cloud-native development.

Coffee and laptop
Image credits : 
freephotocc via Pixabay CC0
x

Subscribe now

Get the highlights in your inbox every week.

In late May, Java celebrated its 25th anniversary, and to commemorate the occasion, developers around the world used the hashtag #MovedByJava to share their achievements, memories, and insights with the programming language.

Over the years, many technologies and trends have contributed to the Java stack's development, deployment, and ability to run multiple applications on standard application servers. Building container images for Kubernetes enables Java developers to package and deploy microservices in multiple cloud environments rather than running several application servers on virtual machines.

With these technologies, the Java application stack has been optimized to run larger heaps and highly dynamic frameworks that can make decisions at runtime. Unfortunately, those efforts weren't good enough to make Java the preferred programming language for developers to implement cloud-native Java applications for serverless and event-driven platforms. Other languages filled in the space, particularly JavaScript, Python, and Go, with Rust and WebAssembly offering new alternatives.

Despite this competition, cloud-native Java is making an impact on cloud-centric software development. Luckily, new Java frameworks (e.g., Quarkus, Micronaut, and Helidon) have recently broken through the challenges by offering smaller applications that compile faster and are designed with distributed systems in mind.

How to install Java on macOS

This future for Java development starts with more people installing and using Java. So I will walk through installing and getting started with the Java development environment on macOS. (If you are running Linux, please see Seth Kenlon's article How to install Java on Linux.)

Install OpenJDK from a Brew repository

Homebrew is the de-facto standard package manager for macOS. If you haven't installed it yet, Matthew Broberg's Introduction to Homebrew walks you through the steps.

Once you have Homebrew on your Mac, use the brew command to install OpenJDK, which is the open source way to write Java applications:

$ brew cask install java

In just a few minutes, you will see:

🍺 java was successfully installed!

Confirm that OpenJDK installed correctly with $ java -version:

$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7)
OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing

The output confirms OpenJDK 14 (the latest version, as of this writing) is installed.

Install OpenJDK from a binary

If you are not a fan of package management and prefer managing Java yourself, there's always the option to download and install it manually.

I found a download link to the latest version on the OpenJDK homepage. Download the OpenJDK 14 binary:

$ wget https://download.java.net/java/GA/jdk14.0.1/664493ef4a6946b186ff29eb326336a2/7/GPL/openjdk-14.0.1_osx-x64_bin.tar.gz

Move to the directory where you downloaded the binary file and extract it:

$ tar -xf openjdk-14.0.1_osx-x64_bin.tar.gz

Next, add Java to your PATH:

$ export PATH=$PWD/jdk-14.0.1.jdk/Contents/Home/bin:$PATH

Also, add this to the path to your dotfiles, .bash_profile or .zshrc depending on what shell you are running. You can learn more about configuring the $PATH variable in How to set your $PATH variable in Linux.

Finally, verify your OpenJDK 14 installation:

$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7)
OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)

Write your first Java microservice on a Mac

Now you are ready to develop a cloud-native Java application with OpenJDK stack on macOS. In this how-to, you'll create a new Java project on Quarkus that exposes a REST API using dependency injection.

You will need Maven, a popular Java dependency manager, to start. Install it from Maven's website or using Homebrew with brew install maven.

Execute the following Maven commands to configure a Quarkus project and create a simple web app:

$ mvn io.quarkus:quarkus-maven-plugin:1.5.1.Final:create \
    -DprojectGroupId=com.example \
    -DprojectArtifactId=getting-started \
    -DclassName="com.example.GreetingResource" \
    -Dpath="/hello"
cd getting-started

Run the application:

$ ./mvnw quarkus:dev

You will see this output when the application starts:

__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \  
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/  
2020-06-13 00:03:06,413 INFO  [io.quarkus] (Quarkus Main Thread) getting-started 1.0-SNAPSHOT on JVM (powered by Quarkus 1.5.1.Final) started in 1.125s. Listening on: http://0.0.0.0:8080
2020-06-13 00:03:06,416 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-06-13 00:03:06,416 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]

Access the REST endpoint using the curl command:

$ curl -w "\n" http://localhost:8080/hello
hello

Congratulations! You have quickly gone from not even having Java installed to building your first web application using Maven and Quarkus.

What to do next with Java

Java is a mature programming language that continues to grow in popularity through new frameworks designed for cloud-native application development.

If you are on the path toward building that future, you may be interested in more practical Quarkus development lessons or other modern frameworks. No matter what you're building, the next step is configuring your text editor. Read my tutorial on Writing Java with Quarkus in VS Code, then explore what else you can do.

Coffee beans

Embrace Java applications on your desktop, and run them on all of your desktops.
Person drinking a hot drink at the computer

Java must include these benefits to compete in the era of cloud-native, microservices, and serverless applications.
Gift box opens with colors coming out

The Homebrew package manager makes it easy to install and update applications and utilities on a Mac.
Person drinking a hot drink at the computer

In this tutorial, I'll walk you through how to rebuild, package, and deploy cloud-native applications automatically with Quarkus.

Topics

About the author

Daniel Oh - Technical Marketing, DevOps Evangelist, CNCF Ambassador, Developer, Public Speaker, Author, Quarkus, Red Hat Runtimes