This operation couldnt be completed. Unable to locate a Java Runtime. [macOS]


If you purchased a new Mac device (Macbook, iMac, Mac mini) and try running java or javac command in Terminal you will get the below error as there is no Java Runtime pre-installed.

[email protected] ~ % java
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

[email protected] ~ % javac
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
Unable to locate Java Runtime macOS Error
Unable to locate Java Runtime macOS Error

You need to visit http://www.java.com and download java dmg setup and install.

Note: Oracle Java is not yet available for MacBooks/Mac Mini's that are running on Apple Silicon M1 Chips, this setup will work using Rosetta 2 - which will convert the code for you!
Intel-based Mac running Mac OS X 10.7.3 (Lion) or later.