Easily switch Java versions with alias on macOS

Setting

Set an alias in .zshrc etc. that sets the environment variables JAVA_HOME and PATH. Set for each installed Java Virtual Machine (JVM).

# AdoptOpenJDK 8
alias setjava8="export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home; export PATH=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin:$(echo ${PATH})"

# AdoptOpenJDK 11
alias setjava11="export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home; export PATH=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin:$(echo ${PATH})"

# AdoptOpenJDK 14
alias setjava14="export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home; export PATH=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home/bin:$(echo ${PATH})"

# Amazon Corretto 8
alias setcorretto8="export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home; export PATH=/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home/bin:$(echo ${PATH})"

# Zulu OpenJDK 13
alias setzulu13="export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-13.jdk/Contents/Home; export PATH=/Library/Java/JavaVirtualMachines/zulu-13.jdk/Contents/Home/bin:$(echo ${PATH})"

Switch Java version

You can switch the Java version by entering the set alias name.

$ setjava14

Check the environment variables and java -version to see if they are switched.

$ echo; echo JAVA_HOME=${JAVA_HOME}; echo; echo PATH=${PATH}; echo; echo 'java -version'; java -version

JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home

PATH=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)

Disadvantage

--Since the environment variable PATH is expanded when the alias is set, the length of the PATH set by the alias may increase each time the shell is started.

Recommended Posts

Easily switch Java versions with alias on macOS
How to switch Java versions on Mac
Install Java 11 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install Java 14 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Switch java with direnv
Install Java 8 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Switch between multiple versions of Java on Mac
Switch versions with alternatives after yum install java
Install Java 8 (OpenJDK: Amazon Corretto) on macOS with Homebrew
Install Java 8 (OpenJDK: Zulu Community) on macOS with Homebrew
Java version control on macOS
Seasonal display with Java switch
Using multiple versions of Java with Brew on Mac + jEnv
[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.
[Java] Branch enum with switch statement
Easily Docker Java applications with Jib
How to switch Java version with direnv in terminal on Mac
Upgrade and switch Ruby versions on Windows 10
Using JupyterLab + Java with WSL on Windows 10
Java version control with jenv on OSX
Install Java8 with Yum on Amazon Linux
Build OpenCV with Java Wrapper on Ubuntu 18.04
How to switch between multiple Java versions
Downgrade Java MacOS
Java switch statement
Using Java 8 with Bluemix (on Liberty Runtime & DevOps Service)
Build Java development environment with VS Code on Mac
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java
Manage multiple versions of Ruby on Mac [macOS + rbenv]