How to download and install Java on Mac

Lets go to step by step guide on how to download and install Java and test a simple application.

Table of Contents
  1. Download Java
  2. Install Java
  3. Verify the Java version
  4. Setup JAVA_HOME
  5. Testing a simple “Hello World” java program on terminal
    1. References

Download Java

  1. Go to Google , and enter “java oracle download mac” on the search bar and press Enter. You will see the first option is from oracle.com for Java Downloads.
Google search for "java oracle download mac"

2. Once you click the link , you see the below page where you see versions available. At time of creation of this post, the Java versions that are available are Java 22 , Java 21 , Java 17. It is recommended to use Long Term Support versions for your java installations and use. To know more, check out my blogpost on the same. https://learnconceptsandcode.com/java_lts_non-lts_versions/.

3. I have selected JDK21 and mac OS tab. We will focus here on the DMG Installer. If you have Apple M1 or M2 chip you can select “ARM64 DMG Installer” and if you have intel chip you will download “x64 DMG Installer“. You can find out if you have M1/M2/Intel chip , by clicking on the Apple icon on top-left corner of your MacBook and select “About this Mac“.

4. Since I have the Apple M2 chip, I have chosen ARM64 DMG Installer.

5. Once you download the DMG installer, and you click the file you see a folder like this one.

Install Java

Once you click on this pkg folder , it will start the installation process and you will see screens as below. You follow thru the installation process, it will also ask you for your Mac password , to be able to continue through the installation process. Once completed, you will see “Install Succeeded“. Once Install is succeeded , you can choose to keep the installer or not.

Verify the Java version

Now that the installation is complete, we can verify this by checking on the terminal with version command and you will see the java version “21.0.3” in this case. [ java -version] command

Setup JAVA_HOME

In order to setup JAVA_HOME we need to follow set of steps and execute a couple of commands.

  1. Enter “touch ./zshrc” which will create a new file for z shell.
  2. Enter “open ./zshrc“. Write “export JAVA_HOME=$(/usr/libexec/java_home)” in the file and save it.
  3. Execute source ./zshrc.
  4. echo $JAVA_HOME which will print the value of JAVA_HOME variable.

Testing a simple “Hello World” java program on terminal

This is the code to print “Hello World” in java. Write below code in HelloWorld.java file. Use these commands

javac HelloWorld.java – To compile the java file

java HelloWorld – To run the java file

References

https://www.oracle.com/java/technologies/downloads

https://docs.oracle.com/en/java/javase/22/install/installation-jdk-macos.html#GUID-2FE451B0-9572-4E38-A1A5-568B77B146DE

Hope this article helps you successfully download and install Java on Mac. And also be able to set JAVA_HOME and run simple java program on terminal. Happy Coding!

By Sarah