🧠
MY SECOND BRAIN
meBlogLinkedInGitHub
  • Second brain
  • AI - Artificial Intelligent
    • AI-labeling
    • AI-training
  • Books
  • Code
    • Linux
    • Gradle
  • Company
    • Interview
  • Computer science
    • Data Structures
    • Algorithms
    • Concurrency
  • Container
    • Docker
      • Docker | Private Docker Registry
    • Kubernetes
  • Distributed systems
    • Akka
    • Analytics
    • Delivery guarantee
    • Kafka
    • Rebalancing
    • RPC
      • gRPC
  • Food
    • Recipes
      • Tiramisu
  • Git
  • GH CLI
    • GH CLI | Pull Request
  • SSH
    • SSH bastion | SSH Jump host
    • SCP
  • Learning
  • Management
  • Reactive systems
  • System Design
    • CAP Theorem
    • Domain Driven Design
    • System Design Interview
    • Load Balancing
    • CDN
  • OCR
  • Productivity
    • Alfred
  • Health
    • Teeth
  • Devops
  • Data stores
    • Elasticsearch
    • Mongo
  • Germany
    • Berlin
      • Where is to buy Christmas trees in Berlin
    • Internet in Germany
      • Install custom router for telekom
  • Transport
    • Bikes
  • Travel
    • Russia
      • Moscow
        • Moscow Attractions
    • United Kingdom
  • Writing
    • Markdown
      • Markdown Tables
  • Programming languages
    • Java
      • Java | OCR
      • Java | Spring
      • JAVA | How to install multiple Java versions on macOS
    • Go
    • Kotlin
    • Python
  • Optimization
    • Email
      • Zero inbox
  • Finance
    • Investment
      • Online brokers
  • People
  • Security
    • SaaS Security
  • Unix
    • Shell
      • ZSH
  • Work
    • Feedback
Powered by GitBook
On this page
  • Preconditions
  • Step 1: Instal JDK(s)
  • Step 2: Make JDK searchable
  • Step 3: Add Java paths to environment variables
  • Step 4: Conclusion and example of usage
  • Links
  1. Programming languages
  2. Java

JAVA | How to install multiple Java versions on macOS

PreviousJava | SpringNextGo

Last updated 1 year ago

Preconditions

  • you're running on macOS

  • is installed

Step 1: Instal JDK(s)

Search for available JDKs

brew search --formulae openjdk.java

install a required version, e.g.

brew install openjdk@8
brew install openjdk@11

Step 2: Make JDK searchable

For the system, Java wrappers to find this JDK, symlink it with. Instead of openjdk@8 use your version.

sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

Step 3: Add Java paths to environment variables

Add to the end of your file which is used to be running by default when the shell session is started. E.g. to the ~/.bashrc or ~/.zshrc

# Defining Java home
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java17='export JAVA_HOME=$JAVA_17_HOME'

#default java17
export JAVA_HOME=$JAVA_17_HOME

Step 4: Conclusion and example of usage

Now by default, you will have Java 17 available

java --version
openjdk 17.0.8 2023-07-18
OpenJDK Runtime Environment Homebrew (build 17.0.8+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.8+0, mixed mode, sharing)

and to switch to other versions, you can simply call java8 or java11

java11
java --version
openjdk 11.0.20 2023-07-18
OpenJDK Runtime Environment Homebrew (build 11.0.20+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.20+0, mixed mode)

Links

brew
Stackoverflow: Mac OS X and multiple Java versions
Medium: Installing & switching between multiple JDK on macOS