How to install and use multiple versions of Java in MacOS

Install different versions of JDK and JRE in local and switch between them easily.

Go to below jdk 14 downloads page at the URL https://www.oracle.com/java/technologies/javase/jdk14-archive-downloads.html. You can also reach this page by searching in google for the same. 

Click on the download symbol corresponding to your installer. Here it is macOS Installer.

Once you click , Accept the licence and download the dmg file corresponding to macOS Installer as shown in the below screenshot.

Then it will ask you to login to oracle account to start downloading the file. If you don’t have an Oracle account, you need to create one for downloading the file.

Once the download is complete, you will have below .dmg file in your Downloads.

Double click the .dmg file .A new Finder window appears that contains an icon of an open box with .pkg file extension.

Double click the .pkg icon to start the application installation  and it displays the introduction window. Click Continue on that window.

Installation type window appear and click Install.

A window appears that displays the below message where you need to enter the administrator username and password and then click Install Software.

Software is installed and confirmation window is displayed.

Once you click Close, it will prompt you to either move the installer to bin or keep it. You can decide based on your disk storage availability.

When you install a new version, that becomes the default JDK version of your machine. You can verify the same by running below command in the terminal which displays the path to current java home.

When you install a new version, that becomes the default JDK version of your machine. You can verify the same by running below command in the terminal which displays the path to current java home.

/usr/libexec/java_home

If you have multiple versions of java installed on your machine, then you can view the same and its corresponsing path by running the below command.

/usr/libexec/java_home -V

There are multiple versions of java available in this machine. If you want to switch between these different versions, we will add the command with an alias to the bash_profile file so that we can easily switch between these versions.

Now we need to open the bash profile file. First we will verify whether a file with that name is already present in the machine or not. Else we need to create the same.

Navigate to the home folder by typing cd ~/ in the terminal. Now type ls -al to list down the files and verify that .bash_profile is been listed.

If it is not present, type touch .bash_profile  in the terminal and the file will be created for you.

Now open this file by using the command open -e .bash_profile in terminal to open the file for editing in TextEditor.

Now add below lines to bash_profile and save the file.

alias j14="export JAVA_HOME=`/usr/libexec/java_home -v 14`; java -version"

alias j13="export JAVA_HOME=`/usr/libexec/java_home -v 13`; java -version"

alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"

alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"

alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"

alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"

alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"

Now we need to run source .bash_profile to reload the bash_profile so that our new changes becomes effective.

Now we can switch to different versions by typing the alias name in the terminal. For Example, type the alias name j12 in the terminal and hit enter. You can verify the current java version by running the command java -version and javac -version. If you want to switch to any other version , you can type the alias name for that version.

You can even echo the java home by typing echo $JAVA_HOME 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)