• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Install Java on macOS

Written by Rahul, Updated on November 25, 2020

Java is a general-purpose, secure, robust, object-oriented language developed by Sun Microsystems in 1990. Java is portable which means it follows to write once run anywhere paradigm. Many of the useful application are build on Java and required Java runtime environment. If you are preparing your system for the developing new java applications, you need to install JDK on your machine.

The latest version is Java 15 which was released in September 15, 2020. This tutorial helps you to install Java on macOS system.

Step 1 – Prerequisites

Before starting the installation of Java using this tutorial you must have the following prerequisites

  • Terminal: You must have Mac Terminal access and little knowledge about working with the terminal application. Ao login to your Mac system and open terminal
  • Homebrew: This tutorial relies on Homebrew, So you must have homebrew installed. Homebrew can be installed with a single command.
  • ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    For more instruction visit Homebrew installation tutorial.

Step 2 – Install JAVA on macOS

At the time of writing this tutorial the available version, Java 11 LTS and Java 15 latest for the installation.

  1. Tap adoptopenjdk/openjdk- Use the following commands to view the details about java versions to be install.
    brew tap adoptopenjdk/openjdk
    
  2. Search Available JDK- View all the available Java versions to install
    brew search jdk
    
  3. Install Java – Use one of the following commands to install Java 8, 9, 10, 11, 12, 13, 14 and 15. Generally, we required Java 8, 11 LTS versions.
    brew cask install adoptopenjdk8  		 ##Install JDK 8  
    brew cask install adoptopenjdk9	 		 ##Install JDK 9  
    brew cask install adoptopenjdk10 		 ##Install JDK 10  
    brew cask install adoptopenjdk11 		 ##Install JDK 11  
    brew cask install adoptopenjdk12 		 ##Install JDK 12  
    brew cask install adoptopenjdk13 		 ##Install JDK 13  
    brew cask install adoptopenjdk14 		 ##Install JDK 14  
    brew cask install adoptopenjdk15 		 ##Install JDK 15  
    

    The installation process may take some time to complete depending on your network speed.

  4. Check Version – Once the installation finished, verify the installed Java version.
    java -version 
    
    openjdk version "15.0.1" 2020-10-20
    OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.1+9)
    OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15.0.1+9, mixed mode, sharing)
    

    Default latest installed version will be active on your machine.

Step 3 – Switching Between Java Version’s

You system may have installed multiple Java versions. For example, I have installed Java 8, 11 and 15 on my system. To view the installed Java version execute:

/usr/libexec/java_home -V 

Matching Java Virtual Machines (3):
    15.0.1, x86_64:	"AdoptOpenJDK 15"	/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
    11.0.9.1, x86_64:	"AdoptOpenJDK 11"	/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_275, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

Create or edit the existing ~/.bash_profile in your favorite text editor. Then export JAVA_HOME environment variable to specific java version, you want to use as default on Mac.

vim ~/.bash_profile 
export JAVA_8=$(/usr/libexec/java_home -v1.8)
export JAVA_11=$(/usr/libexec/java_home -v11)
export JAVA_15=$(/usr/libexec/java_home -v15)

# Java 8
#export JAVA_HOME=$JAVA_8

# Java 11
export JAVA_HOME=$JAVA_11

# Java 15
# export JAVA_HOME=$JAVA_15

Save file and close it. The above settings will make JAVA_HOME point to JAVA 11.

Execute the following command to reload environment.

source ~/.bash_profile 

All done, check the current active java version

java -version 

openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

When you want to switch JDK version again, update the ~/.bash_profile and export JAVA_HOME to other JDK version.

Conclusion

This tutorial explained you to how to install Java on macOS. Also provides you steps to switching between multiple installed Java versions.

You can visit Github page of the OpenJDK casks.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

13 Comments

  1. Avatar Pradipta Sarma Reply
    November 6, 2020 at 12:03 pm

    caskroom has moved to homebrew.
    So: brew tap homebrew/cask

  2. Avatar Jim Reply
    January 2, 2020 at 8:32 am

    If anyone else is having problems with brew tap caskroom/cask, don’t brew tap just use brew cask and install java with `brew cask install java`.

  3. Avatar Ram Reply
    October 10, 2019 at 11:41 pm

    For macos mojave

    brew tap homebrew/cask-versions
    brew cask info java11
    brew cask install java11

    • Avatar Mateo Reply
      November 1, 2019 at 11:14 pm

      And still `brew cask install java` for java 13

  4. Avatar Ruth Reply
    October 7, 2019 at 3:32 pm

    how do you install java12 now that java 13 will be installed when using “java”?

    • Rahul Rahul Reply
      October 8, 2019 at 4:28 am

      Thanks, Ruth, The article has been updated with latest versions.

  5. Avatar john Reply
    August 16, 2019 at 12:40 pm

    Just go to https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html
    with an Oracle account….

  6. Avatar matej simunic Reply
    May 20, 2019 at 10:12 am

    perfect, ty

  7. Avatar Robert Reply
    August 11, 2018 at 5:43 pm

    Worked perfectly on my MAC. Thank you!!

  8. Avatar Robert Reply
    August 11, 2018 at 5:42 pm

    Worked Perfectly for my on my MAC. Thank you for this.

  9. Avatar T Reply
    July 15, 2018 at 11:43 am

    Thanks

  10. Avatar BobK Reply
    May 8, 2018 at 9:30 pm

    Perfecto! Thankyousir.

  11. Avatar Tyler Reply
    February 15, 2018 at 12:48 am

    Thanks a lot… Got Java installed successfully on my Mac

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy