Terminal Java Error:
Error Screenshot:

Reason for the Error:
If you have recently purchased a Mac, or simply formatted your macOS to its factory settings, then by default there is no Java JDK (or JVM) pre-installed on your Mac device.
Hence, when you open the Terminal and run a java or javac command, you will get an error saying this operation couldn’t be completed as the System is unable to locate a Java Runtime.
How to resolve the Error:
- Java 8
- Java 11
- Java 17
- Java 21
- OpenJDK
- Oracle JDK
- AdoptOpenJDK
- Azul Zulu
- Amazon Corretto
- IBM JDK
- SAP Machine
- Red Hat OpenJDK
- Microsoft Build of OpenJDK
- BellSoft Liberica JDK
- GraalVM
- Make sure brew is installed on your Mac. Open Terminal and run the below command to check.
% brew --version zsh: command not found: brew
If you get the above error, instead of version details, follow this article to install brew on your Mac - https://code2care.org/howto/install-homebrew-brew-on-m1-mac
- Now simply run the below commands to install Java 8/11/17,
brew install openjdk@8
brew install openjdk@11
brew install openjdk@17
To install Amazon Coretto Java JDK
brew install --cask corretto
To install Graal VM Java JDK
brew install --cask graalvm-jdk
To install Oracle Java JDK
brew install --cask oracle-jdk
To install Azul Zulu Java JDK
brew install --cask zulu
- Open the web browser and go to : https://aws.amazon.com/corretto
- Now click on "Download Amazon Corretto 17"
- Now, download .pkg file macOS x64 or macOS aarch64 based on your Mac Type.
- Open the "amazon-corretto-17-aarch64-macos-jdk.pkg" file from your downloads folder and complete the installations.
- Open Terminal and you should be able to use Java commands.
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
- How to empty trash in Android Device - Android
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- How to List all Packages installed using pip [Python] - Python
- JSON Text to JavaScript Object using eval() Example: JSON Tutorial - Json-Tutorial
- Fix: Ubuntu (Linux) - bash: sudo: command not found error - Ubuntu
- Notepad++ Search Across Multiple Lines - NotepadPlusPlus
- Select Line Number TextEdit on Mac - MacOS
- How to Perform Cut/Paste Operations in Nano Editor - Linux
To resolve this Java error, you will need to download and install Java JDK or JRE on your Mac.
As of year 2023, you can install Java versions from Java 8 to Java 21 on your device. It is recommended to get a stable LTS (Long Term Supported) version of Java.
List of Java LTS Versions:
Also, depending upon what type of Mac device you have - Intel Based / M1/M2 Max/Ultra you would need a different kind of setup to run Java natively on your device.
Mac Type vs Java installations:
Mac Chip/Processor Type | Java Compatibility |
---|---|
Apple Silicon (M1/M2) | ARM64 (Java 11 and later) |
Intel | x86 x86_64 (Java 8 and later) |
Finally there is a list of Java distributions to choose from,
Java 8/11/17 Installation Steps
Instead of download the setup from a website, we can easily install Java on your Mac using package manager like Homebrew.
To install OpenJDK Java
Install Java by download DMG Setup file
If for some reasons you are having issues with brew, you can download Java JDK from the internet as well, we will see an example of how to download and install Amazon AWS Corretto JDK version 17 which is a free of cost distribution and requires no login or email.
If you prefer other distributions.
JDK Distribution | Provider Java download link |
---|---|
OpenJDK | https://openjdk.java.net/ |
Oracle JDK | https://www.oracle.com/java/technologies/javase-downloads.html |
AdoptOpenJDK (Adoptium) | https://adoptium.net/ |
Azul Zulu | https://www.azul.com/downloads/zulu/ |
Amazon Corretto | https://aws.amazon.com/corretto/ |
IBM JDK | https://developer.ibm.com/languages/java/ |
SAP Machine | https://github.com/SAP/SapMachine |
Red Hat OpenJDK | https://developers.redhat.com/products/openjdk/overview |
Microsoft Build of OpenJDK | https://docs.microsoft.com/en-us/java/openjdk/ |
BellSoft Liberica JDK | https://bell-sw.com/ |
GraalVM | https://www.graalvm.org/ |