Install multiple versions of the Java JDK on MacOS.

Author
By Darío Rivera
Posted On in Java

Surely at this moment you need to install more than one version of the famous JDK on your MacOS machine. Today we will see how this is possible and how to switch between versions with just one command.

Installation of new versions

The first step is to follow our tutorial onJDK (Java Development Kit) Installation on MacOS. Once you have done this, you must install a new version just as you installed the first one. Download the version from the following link:

http://jdk.java.net/

Once you have done this, you place it in the /Library/Java/JavaVirtualMachines folder. This way, our new or maybe older version of the JDK is already installed.

Switch between versions

By default, MacOS takes the latest version from the java virtual machines folder. To indicate to the system which version to take, we must configure the JAVA_HOME environment variable. For example, if we wanted to use JDK 11, the instruction would be as follows:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home

Configure version switching

If you want to have a specific version every time you log in, you must modify your terminal configuration file. If you use zsh (the default in MacOS), you must add the variable export instruction to the end of the ~/.zshrc file.

It is also quite useful to create an alias to quickly switch between versions so that with just typing a command like the following you can change your version:

javause 19

To achieve this, you can use this script and paste it into your ~/.zshrc file.

alias javause="_javause"

function _javause() {
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-$1.jdk/Contents/Home;
}

Security configuration

When you change the default JDK version, the system usually won't let you run the new version because it is from an unknown and not secure developer. To be able to run this version, you must go to SystemPreferences > Security & Privacy and go to the General tab. Once there, click on Unlock and click on the radial button App Store and Identified Developers. Find the application you want to run and click Open Anyway.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.