How to install Java on a Mac

How to install Java on a Mac

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.

No alt text provided for this image

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., QuarkusMicronaut, 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: https://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" https://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.

This article is published by myself originally at https://opensource.com/article/20/7/install-java-mac

要查看或添加评论,请登录

Daniel Oh的更多文章

  • Combine GraphQL with Java to build a flexible and modern API

    Combine GraphQL with Java to build a flexible and modern API

    In the past few years, developers have used RESTful web services over HTTP(s) to expose business functions using an…

  • What makes Java open source?

    What makes Java open source?

    If you're using Java to write business applications, you may know that Java Standard Edition (SE) is not open source…

  • 5 open source IDE tools for Java

    5 open source IDE tools for Java

    Java frameworks make life easier for programmers by streamlining their work. These frameworks were designed and…

  • Set up Vim as your Rust IDE

    Set up Vim as your Rust IDE

    The Rust programming language is designed to implement systems programming with safe concurrency and high memory…

  • Developing Java in Quarkus, an open source IDE

    Developing Java in Quarkus, an open source IDE

    In the previous articles, you learned about 6 requirements of cloud-native software and 4 things cloud-native Java must…

  • 4 things cloud-native Java must provide

    4 things cloud-native Java must provide

    Java is still the pervasive development language among enterprise developers, even though it is not developers'…

  • 6 requirements of cloud-native software

    6 requirements of cloud-native software

    For many years, monolithic applications were the standard enterprise architecture for achieving business requirements…

  • 3 types of metric dashboards for DevOps teams

    3 types of metric dashboards for DevOps teams

    Metrics dashboards enable DevOps teams to monitor the entire DevOps platform so they can respond to issues in…

  • Certifications for DevOps engineers

    Certifications for DevOps engineers

    DevOps teams appreciate using DevOps processes, especially in multi- and hybrid cloud infrastructures, for many…

  • 5 reasons to use Kubernetes

    5 reasons to use Kubernetes

    Kubernetes is the de facto open source container orchestration tool for enterprises. It provides application…

社区洞察

其他会员也浏览了