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.
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- [fix] macOS Ventura xcrun: error: invalid active developer path missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun - MacOS
- 27: Measure Elapsed Time for a Python Program Execution - Python-Programs
- Hide cURL Outputs and Errors on Terminal - cURL
- How to List all Packages installed using pip [Python] - Python
- Deep Dive into Java 8 Predicate Interface - Java
- Fix: Cannot contact reCAPTCHA. Check your connection and try again. - Google
- SharePoint formula - Calculated columns cannot contain volatile functions like Today and Me - SharePoint
- Take Screenshots on Mac OS without Keyboard - Mac-OS-X
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/ |
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org