StackTips

How to Install Java for MacOS and Windows?

siteadmin avtar
Stacktips   |   #Java   |   Sep 25, 2023, 

As a developer you have two choices when it comes to installing Java, Oracle JDK or OpenJDK.

Oracle offers the commercial version Java JDK. It has all the features of Open JDK, plus some proprietary features.

Open JDK is the open source, free to use for both personal and commercial software development. It has all the features of Oracle JDK, minus some proprietary features and commercial support from Oracle. Generally it is considered to have slightly lower performance than Oracle JDK, but still very good and widely used in the industry.

There are many different distributions of OpenJDK available, some of the popular distributions are Amazon Corretto, Microsoft JDK, Azul Zulu, and Red Hat OpenJDK.

In this article, we show you how to install Java on Mac using SDKMan and Homebrew and using windows installer.

Install Java Using SDKMan

SDKMan command line tool used for managing multiple versions of SDKs and building tools on a single system. This works for MacOS and Linux, Windows WSL. 

SDKMan simplifies the installation, management, and switching between different versions of Java SDK effortlessly. To install the JDK using SDKMan, first, let us get a list of available Java SDKs

sdk list java

This will return a list of all available Java versions


You can filter a specific Java version. i.e. the following command will filter out all Java 17 candidates.

sdk list java | grep 17

Now to install a specific version, you need to use the following command. Optionally you can use the identifier to install a specific version. E.g.

sdk install java 21-amzn

Check the list of installed Java versions

sdk list java | grep installed

Use specific versions of Java

sdk use java 21-amzn

Install Java in Windows OS

For instructions on how to install using the graphical PKG and MSI installers, or through package managers WinGetHomebrewapt, and yum, see the Install page.

To ensure that Java is installed correctly, open a Command Prompt or PowerShell window and enter the following command.

java -version

Install Java using Homebrew

  • Type brew search openjdk to find all available Java-related formulas.
$ brew search openjdk
==> Formulae
openjdk ✔           openjdk@11          openjdk@17 ✔        openjdk@8

==> Casks
adoptopenjdk       homebrew/cask-versions/adoptopenjdk8
microsoft-openjdk  homebrew/cask-versions/microsoft-openjdk11
openkey

To show the formula details, you can use the brew info command. The Java formula always contains the latest OpenJDK version; at the time of writing, the latest is JDK 17.

The openjdk@17 formula contains the Java 17LTS version

$ brew info openjdk@17
==> openjdk@17: stable 17.0.4.1 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
/usr/local/Cellar/openjdk@17/17.0.4.1_1 (639 files, 305.6MB)
  Poured from bottle on 2023-09-25 at 00:00:57
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/[email protected]
License: GPL-2.0-only with Classpath-exception-2.0
==> Dependencies
Build: autoconf ✘
==> Requirements
Build: Xcode ✔
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

openjdk@17 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@17 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@17 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@17/include"

==> Analytics
install: 30,649 (30 days), 82,462 (90 days), 154,946 (365 days)
install-on-request: 20,080 (30 days), 52,615 (90 days), 88,978 (365 days)
build-error: 65 (30 days)

Now install the Open JDK 17:

brew install openjdk@17

Resources

https://learn.microsoft.com/en-us/java/openjdk/install

siteadmin avtar

Stacktips

Provides a developer-friendly way to learn programming