Understanding the Context
- Why Avoid Xcode? Xcode is a large development environment that includes compilers and tools for macOS. It’s not usually necessary just to run Java programs (or use OpenJDK).
- OpenJDK Distributions: You’ll typically download an OpenJDK distribution from a provider like Oracle, Adoptium (Eclipse Temurin), Amazon Corretto, or Azul Zulu. This guide is generally applicable regardless of your choice of OpenJDK distro.
.tar.gzArchive: You’ll likely be downloading a.tar.gzarchive because most providers don’t offer.pkginstallers for OpenJDK on macOS for the latest versions.
Steps to Install OpenJDK 21 via .tar.gz on macOS
-
Download OpenJDK 21
.tar.gz- Choose a Provider: Go to the website of your preferred OpenJDK provider. Here are a few popular options:
- Oracle OpenJDK: https://jdk.java.net/ (Look for “JDK 21 General-Availability Releases”)
- Adoptium (Eclipse Temurin): https://adoptium.net/ (Select “JDK 21”, your OS (macOS), and your architecture (e.g., x64))
- Amazon Corretto: https://aws.amazon.com/corretto/ (Select “Amazon Corretto 21”)
- Azul Zulu: https://www.azul.com/downloads/zulu/ (Select “Java 21 (LTS)”)
- Download the Correct File: Make sure you download the
.tar.gzfile for macOS. The file name will typically contain “macos”, “darwin”, or similar. Note the exact name for the next step. I’ll assume the file is in the~/Downloadsdirectory.
- Choose a Provider: Go to the website of your preferred OpenJDK provider. Here are a few popular options:
-
Open Terminal
- Find it in
/Applications/Utilities/Terminal.appor search with Spotlight (Cmd+Spacebar).
- Find it in
-
Extract the
.tar.gzFilecd ~/Downloads tar -xzf <name-of-downloaded-file>.tar.gz- Replace
<name-of-downloaded-file>.tar.gzwith the actual file name you downloaded. For example, it might look like:jdk-21.0.2_osx-x64_bin.tar.gzor similar. - After extraction, a new directory will be created in the
~/Downloadsdirectory. Its name will be similar to the.tar.gzfilename, usually without the file extensions. Let’s assume it’s called something likejdk-21.0.2.
- Replace
-
Move the Extracted Directory (Optional, But Recommended)
-
It’s generally good practice to move the extracted JDK directory to a more conventional location. Here’s how to move it to
/Library/Java/JavaVirtualMachines/:sudo mkdir -p /Library/Java/JavaVirtualMachines sudo mv ~/Downloads/jdk-21.0.2 /Library/Java/JavaVirtualMachines/- You might have to change
jdk-21.0.2to match your extracted directory name. sudois required because moving things in the/Libraryfolder require administrator privileges. You’ll be prompted for your password.
- You might have to change
-
-
Set the
JAVA_HOMEEnvironment Variable-
JAVA_HOMEis an environment variable that points to the root directory of your JDK installation. This lets Java-related tools find your JDK. -
Edit your shell profile: The shell profile is a hidden file that contains your environment settings. The typical shell in modern macOS is
zsh. The settings will be contained in a.zshrcfile. You might not have one, and if that’s the case you may have to create one (instructions follow). It will need to be in your home directory. -
Check if
.zshrcalready existsls -al ~ | grep .zshrcIf the output displays a line like this,
... .zshrc, then the.zshrcfile is already present. Otherwise, you can just create one. -
Create or edit
.zshrc:nano ~/.zshrc- If the
.zshrcfile did not exist before, this will create one. nanois a simple command-line text editor.
- If the
-
Add the
JAVA_HOMEvariable: Inside thenanoeditor, add these lines to the end of the file:export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-21.0.2" export PATH="$JAVA_HOME/bin:$PATH"- Important: Make sure
"/Library/Java/JavaVirtualMachines/jdk-21.0.2"matches the exact path where you moved the directory in the previous step (if you moved it) or where the extracted files are if you did not move it.
- Important: Make sure
-
Save and exit nano: Press Ctrl+X, then press ‘Y’ to save changes, and then Enter to confirm the file name.
-
-
Activate the Changes
-
For the changes to take effect immediately, you can use this command:
source ~/.zshrc
-
-
Verify the Installation
-
Open a new Terminal window to make sure the environment is properly set.
-
Run the following commands:
java -version javac -version -
These commands should output the installed OpenJDK version. For example:
openjdk version "21.0.2" 2023-10-17 OpenJDK Runtime Environment (build 21.0.2+13-5) OpenJDK 64-Bit Server VM (build 21.0.2+13-5, mixed mode, sharing) javac 21.0.2- If you see output similar to this, then congratulations! You have successfully installed OpenJDK 21 without needing Xcode.
-
Troubleshooting
java -versionorjavac -versiondon’t work: Double-check the following:- The
JAVA_HOMEvariable in your.zshrcor equivalent profile file is set to the correct path of your extracted OpenJDK directory. - You have activated the
.zshrcsettings (withsource ~/.zshrc) - You have opened a new terminal window.
- The
- Permission Errors: Be careful when using the
sudocommand. Always double check what you’re doing. - Incorrect Path: A common mistake is to not correctly specify the path. Copy and paste is always your friend.
Additional Notes
- Multiple JDKs: You can have multiple JDKs installed on your machine. The
JAVA_HOMEvariable determines which JDK is being used. You may usejenv,sdkmanor similar tools to easily switch between different JDK versions. - Shell Profiles: Different shells (e.g.,
bash,zsh,fish) may use different configuration files. The instructions here are primarily forzsh, but there is a similar mechanism for other shells.
Let me know if you run into any issues or need further clarification. I’m here to help!

