69

Could someone please let me know the steps to install Java on a Mac.

I did brew install java

I get this

Warning: openjdk 17.0.1 is already installed and up-to-date.
To reinstall 17.0.1, run:
  brew reinstall openjdk

If I do java -version, I get this.

openjdk version "13.0.8" 2021-07-20

If I have navigate to /Library/Java, I have 2 empty directories.

Where is java 17 installed??

yglodt
  • 13,807
  • 14
  • 91
  • 127
RamPrakash
  • 2,218
  • 3
  • 25
  • 54
  • 3
    I like [sdkman](https://sdkman.io/). Then `sdk install java 17.0.1.fx-librca` to install the current release from Liberica (with included JavaFX). Or `sdk ls java` to see the various other current releases. – Elliott Frisch Nov 07 '21 at 18:43
  • 1
    +100000 for sdk man as Elliott suggested above. The main benefits of it is that you can have multiple versions installed, switch them whenever you need and (very important in case something goes wrong) they are installed in your home directory. – Augusto Nov 07 '21 at 18:59
  • java 17 is likely found where brew installed it under `/usr/local/Cellar/openjdk` – Chris Oct 01 '22 at 12:22

11 Answers11

164

In 2023, even if you can use just brew..

brew install openjdk@17 

Java will be installed here:

/usr/local/opt/openjdk@17/bin/java

for Apple Silicon path is /opt/homebrew/... rather than /usr/local/...

For the system Java wrappers to find this JDK, symlink it with:

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

resp. for Silicon

sudo ln -sfn /opt/homebrew/opt/openjdk\@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

(This is what you are missing btw, if javac or javac --version does not give you a command-not-found but an The operation couldn’t be completed. Unable to locate a Java Runtime.)

...give a try to sdkman, it's far better than brew

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

then open a new shell and try list to see what you could install ;-)

sdk list java 

At time of writing you could use:

sdk install java 17.0.4.1-tem

Java will be installed here:

/Users/freedev/.sdkman/candidates/java/17.0.4.1-tem
freedev
  • 25,946
  • 8
  • 108
  • 125
  • 7
    sdkman is the way to go! Versions installed with brew are not recognized by Eclipse. – Gert-Jan Mar 11 '22 at 14:44
  • @Gert-Jan so do you think the answer should be updated leaving only `sdkman`? – freedev Mar 11 '22 at 14:46
  • 1
    Not sure about that, brew may be fine for some use cases but doesn't work when you try to configure the installed JDK in Eclipse. – Gert-Jan Mar 11 '22 at 15:26
  • @Gert-Jan the full JDK is in the `libexec/openjdk.jdk/Contents/Home` subdir. – TrogDor Sep 29 '22 at 14:41
  • 2
    sdkman was the best solution for me among all answers. Give it a chance – Gerson Montenegro Jan 01 '23 at 17:39
  • 2
    Can someone explain to me why there are different VENDORS for a programming language? This is mind-boggling to me, if I want a version of Python I get that version. If I want a version of Go, I get that version. For example, if I am on macOS, is one vendor better than another here? – fullStackChris Jan 04 '23 at 18:48
  • 1
    With Homebrew it was installed to: /opt/homebrew/Cellar/openjdk@17 On my machine. – Tbi Jan 06 '23 at 21:23
  • @Tbi Pay attention during the installation there is a message "If you need to have openjdk@17 first in your PATH, run: `echo 'export PATH="/usr/local/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc`". Because `/usr/local/opt/openjdk@17 -> ../Cellar/openjdk@17/17.0.5`. – freedev Jan 07 '23 at 22:31
  • Better how and why? – RukshanJS Apr 09 '23 at 18:17
  • @Tbi is right, for my m1, path was `/opt/homebrew/...` rather than `/usr/local/...` – Black Chase Apr 21 '23 at 19:10
  • @BlackChase thanks for letting me know. I’m going to update the answer. – freedev Apr 22 '23 at 20:07
  • 1
    @RukshanJS IMHO sdkman is more focused on managing software development kits while homebrew is more general-purpose. I understand your comment but I don’t like to add too many infos in my answers (and this is one of the longest answers I have ever written). This is good for clarity, to get the people straight to the point. – freedev Apr 22 '23 at 20:46
  • 1
    @freedev thanks for that clarification. It clears my doubt. Again, thanks for the answer but wanted to have some basic idea why it's better. Cool! – RukshanJS Apr 26 '23 at 03:16
  • 1
    @fullStackChris this is an odd comment. There are actually multiple implementations of python (cython, jython, IronPython), there are multiple implementations of go (gcc-go and gc) and there are multiple implementations of, say, the C language (too many to count). If the language has been well specified, multiple implementations are to be expected. – archgoon Aug 06 '23 at 21:39
24

Java doesn't mind if you install multiple versions. This is often required; java is not backwards compatible (it tries to change little, but e.g. the java8 to java9 transition broke a ton of stuff, much of it needless and much of it not reasonably expectable or fixable by libraries and apps, so a bunch of java apps and libraries only run on java8 - just an example).

So, yes, you have installed JDK17. Also, yes, if you just run java without specifying which one you want, you so happen to get java13 here.

To see all installed javas, you can run:

/usr/libexec/java_home -V

to 'override', you can use something like (depends on which shell you're using on your mac):

export JAVA_HOME=`/usr/libexec/java_home -v 17`

(the backticks mean: Run this then take the output of it and treat that as the 'value' of the expression. here, assign it to the JAVA_HOME env var. -v 17 requests a path to java 17. The -V option lists all and is meant for your eyeballs, not for scripts. The -v option is mostly for scripting, and that's how we're using it here).

JAVA_HOME decides which java is used by some things, but the java you get when you just type java is /usr/bin/java, and that executable is actually just a wrapper that picks a java to run from amongst all installed versions. It uses JAVA_HOME to decide which java to actually run. There are wrappers for all the common commands (javac, too). You can always run e.g. which javac to see what that actually runs; you probably see /usr/bin/javac. Everything in /usr/bin is one of these wrapper thingies that looks at JAVA_HOME and then runs the binary it finds there.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • This works. But when i close the terminal this doesn't get saved, it resets back to the old java version. how to permanently save this? – busuu Mar 02 '23 at 21:30
  • Put a `JAVA_HOME=` command in `~/.profile` or `/etc/profile` - some file that is run by the system itself. – rzwitserloot Mar 02 '23 at 23:41
23

To specify version 17

brew install openjdk@17

Later I add create a link:

sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk \
     /Library/Java/JavaVirtualMachines/openjdk-17.jdk

And use jenv to control which java version to use

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
Ehud Lev
  • 2,461
  • 26
  • 38
9

To used the version installed by homebrew rather than the one installed by the OS you can get detailed information from homebrew by typing

brew info java

Currently it states

For the system Java wrappers to find this JDK, symlink it with sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /opt/homebrew, because macOS provides similar software and installing this software in parallel can cause all kinds of trouble. If you need to have openjdk first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc For compilers to find openjdk you may need to set: export CPPFLAGS="-I/opt/homebrew/opt/openjdk/include"
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
7

For Homebrew, the package that installs the new AdoptOpenJDK is named as temurin. So you need to use below command:

brew install --cask temurin17

If you get any error like temurin cask not available then update brew using below commands:

brew update
brew tap homebrew/cask-versions

To switch quickly between different versions of java add the entries in bashrc as per your jdk versions like:

alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"
Pukhraj soni
  • 1,832
  • 2
  • 17
  • 11
3

Steps to install

You asked:

let me know the steps to install Java on a Mac.

  1. Download an installer free-of-cost from vendors such as Adoptium, Azul Systems, Bellsoft, Amazon, Oracle, Microsoft, SAP, and others.
  2. Run installer app.
  3. Quit the installer app when done.
  4. Verify installation by typing on a command-line in Terminal.app:
    java --version
  5. Delete installer app that you downloaded.
  6. Configure your IDE to use that new Java implementation you installed.

Java location

You asked:

Where is java 17 installed??

/Library/Java/JavaVirtualMachines/

In the Finder, choose Go > Go to Folder, and paste /Library/Java/JavaVirtualMachines/.

Note that this is not the Library folder within your home folder. We are not referring to /Users/your_user_name/Library/…. We are referring to the root Library folder that applies across all the user accounts on this Mac.

You said:

I did brew install java

No need for the Homebrew package manager. If you already enjoy using the brew tool, proceed. But if new to Homebrew, skip it if your only goal is to install Java. Just use an installer for Java as you would for many Mac apps.

JavaFX

You added a tag for javafx.

Be aware that for JavaFX, you have two options:

  • Include the necessary OpenJFX libraries within your development project and within your final app, or
  • Use a JDK that includes the JavaFX/OpenJFX libraries.

At least two vendors provide JDK installers that include the JavaFX/OpenJFX libraries:

  • Azul Systems (ZuluFX)
  • Bellsoft (LibericaFX)
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Is it not 'java -version'? I think you have 2 dashes where there should be 1. – amalgamate Jul 19 '23 at 20:24
  • 1
    @amalgamate Both work, `java -version` and `java --version`. [Both are documented](https://docs.oracle.com/en/java/javase/20/docs/specs/man/java.html#standard-options-for-java) as being standard options. – Basil Bourque Jul 20 '23 at 02:24
2

This answer is specifically if you use Intellij on a Mac

Within IntelliJ, you can use the IDE to add new JDKs of selected versions from common vendors. Instructions for this are here:

When setting up the JDK, you can either:

  1. Select a pre-existing JDK which has been registered with the IDE OR
  2. Use the Add JDK option to add a new JDK which you previously downloaded and installed using the method outlined in Basil's answer OR
  3. Use the Download JDK option to choose a vendor and version of the JDK that the IDE will automatically download, install and make available for selection.
    • The JDKs installed by Idea will be located in the same location as outlined in Basil's answer for a manual install /Library/Java/JavaVirtualMachines/.

One common issue is that the version of the JDK registered for the project differs from the default version used in the terminal. This can sometimes mean that the app works when run in Idea, then fails when run in the terminal (or, at least that it is executed against a version of the JDK you didn't expect).

To select the version of the JDK to run in the terminal, configure the Java home setting as outlined in rzwitserloot's answer.

export JAVA_HOME=`/usr/libexec/java_home -v 17`

Also, some tools such as the openjfx maven plugin will not use the java version selected in Idea when executing a call to a JDK tool like jlink, but will instead have their own mechanism for finding a JDK to use (e.g. look at JAVA_HOME or use the Maven toolchains plugin). So it is always good to check the JAVA_HOME variable and ensure that it is set to a reasonable value, both for terminal execution and for effective use of Java development tools that may rely on it.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I noticed later. It was cool. – RamPrakash Nov 09 '21 at 00:27
  • Mine still doesn't work: "Could not read 'https://api.adoptopenjdk.net/v3/binary/latest/16/ga/mac/aarch64/jdk/hotspot/normal/adoptopenjdk' as it does not exist" – IgorGanapolsky Jan 06 '23 at 20:00
  • The [adoptionjdk](https://adoptopenjdk.net/) site was replaced by [adoptium](https://adoptium.net/temurin/releases/?version=19). The adoptopenjdk site warns you of this when you visit the site. Recent releases are not distributed by adoptopenjdk, in particular it does not have releases for modern non-Intel macs. Adoptium JavaFX 19 includes a release for your architecture. Other distributions such as liberica, zulu, oracle openjfx, etc also have packages for M series macs. – jewelsea Jan 06 '23 at 21:48
2

I think that answers here are not fully out of topic, but from my point of view, my case is exactly the same as that of the author. I had already installed java 8, 11, and 13. All of them resides at:

/Library/Java/JavaVirtualMachines/

Nevertheless when I tried to find the path of JDK 17 it resides:

/usr/local/Cellar/openjdk

I use Mac OS Big Sur and the JDK was installed with homebrew

Peter S.
  • 470
  • 1
  • 7
  • 17
2

Brew now supports searching old formulae and allowing you to install a specific version. I'm using Homebrew 3.5.2-117-gb941470

  1. Create a local tap: brew tap-new --no-git local/openjdk
  2. Ask Brew to find the formulae of the version you want: brew extract --version 17.0.2 openjdk local/openjdk
  3. Install Java 17 using your new local tap: brew install openjdk@17.0.2
  4. Link the JDK into the MacOS JVM Dir: sudo ln -sfn /usr/local/opt/openjdk@17.0.2/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk17.0.2.jdk
  5. Check Java 17 is available: /usr/libexec/java_home -V
  6. Set Java Home: export JAVA_HOME=$(/usr/libexec/java_home -v17)
  7. Check current version of Java is correct...
$ java --version
openjdk 17.0.2 2022-01-18
grbonk
  • 609
  • 6
  • 22
2

I'm currently using SDKMAN on macOS to switch between JAVA 11 and 17 based on the project I'm working on, it's pretty easy

 curl -s "https://get.sdkman.io" | bash 
 source "/users/#USER_HOME/.sdkman/bin/sdkman-init.sh"
 sdk list java
 sdk install java #JAVA_IDENTIFIER
 sdk use java #JAVA_IDENTIFIER

Then each time switch using

 sdk list java
 sdk use java #JAVA_IDENTIFIER

You could also allow your IDE such as Intellij to use the system console bash, so you could switch using SKD command from inside the IDE terminal

Hany Sakr
  • 2,591
  • 28
  • 27
0

The following method installs Java without the need for any additional tools or package managers.

Go to https://jdk.java.net/17/ and download the latest macOS archive.

Download the latest Adoptium release for the Java 17 branch by going to https://adoptium.net/temurin/releases/ and be sure to select the tar.gz version.

The archive is either for x64 (Intel CPU) or for AArch64 (Apple Silicon / M1 CPU).

Then, open a Terminal, and extract the downloaded archive to the system path for Java virtual machines:

cd Downloads
tar xzf OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.5_8.tar.gz -C /Library/Java/JavaVirtualMachines
yglodt
  • 13,807
  • 14
  • 91
  • 127