OpenJDK 21 on macOS without Xcode

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.gz Archive: You’ll likely be downloading a .tar.gz archive because most providers don’t offer .pkg installers for OpenJDK on macOS for the latest versions.

Steps to Install OpenJDK 21 via .tar.gz on macOS

  1. Download OpenJDK 21 .tar.gz

    • Choose a Provider: Go to the website of your preferred OpenJDK provider. Here are a few popular options:
    • Download the Correct File: Make sure you download the .tar.gz file 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 ~/Downloads directory.
  2. Open Terminal

    • Find it in /Applications/Utilities/Terminal.app or search with Spotlight (Cmd+Spacebar).
  3. Extract the .tar.gz File

    cd ~/Downloads
    tar -xzf <name-of-downloaded-file>.tar.gz
    
    • Replace <name-of-downloaded-file>.tar.gz with the actual file name you downloaded. For example, it might look like: jdk-21.0.2_osx-x64_bin.tar.gz or similar.
    • After extraction, a new directory will be created in the ~/Downloads directory. Its name will be similar to the .tar.gz filename, usually without the file extensions. Let’s assume it’s called something like jdk-21.0.2.
  4. 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.2 to match your extracted directory name.
      • sudo is required because moving things in the /Library folder require administrator privileges. You’ll be prompted for your password.
  5. Set the JAVA_HOME Environment Variable

    • JAVA_HOME is 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 .zshrc file. 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 .zshrc already exists

      ls -al ~ | grep .zshrc
      

      If the output displays a line like this, ... .zshrc, then the .zshrc file is already present. Otherwise, you can just create one.

    • Create or edit .zshrc:

      nano ~/.zshrc
      
      • If the .zshrc file did not exist before, this will create one.
      • nano is a simple command-line text editor.
    • Add the JAVA_HOME variable: Inside the nano editor, 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.
    • Save and exit nano: Press Ctrl+X, then press ‘Y’ to save changes, and then Enter to confirm the file name.

  6. Activate the Changes

    • For the changes to take effect immediately, you can use this command:

       source ~/.zshrc
      
  7. 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 -version or javac -version don’t work: Double-check the following:
    • The JAVA_HOME variable in your .zshrc or equivalent profile file is set to the correct path of your extracted OpenJDK directory.
    • You have activated the .zshrc settings (with source ~/.zshrc)
    • You have opened a new terminal window.
  • Permission Errors: Be careful when using the sudo command. 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_HOME variable determines which JDK is being used. You may use jenv, sdkman or 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 for zsh, 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!

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Scroll to Top