How to Install Java on MacOS?
Java is a general-purpose language initially developed by James Gosling at Sun Microsystems. Java is a high-level, object-oriented programming language invented to have very few implementation dependencies feasible. Compiled Java code can run on all platforms that support Java without recompilation, making it easier for programmers as they only have to write once and can run it anywhere. The latest version of Java is Java 17; Java 11 and 8 are the currently supported long-term support (LTS) versions as of September 2021.
Note: The Java 17 is an latest version which means Oracle will support this version for a long-time. The premier support will be available till September 2026 and extended support will be available till September 2029.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Downloading and Installation
Ways: Basically, there are two ways as listed below:
- Downloading it from the Oracle website
- Using terminal via homebrew
Here we will be discussing the first method where java can be downloaded from Oracle’s official Website and can be installed with the help of the following steps:
Step 1: Download Java 17 by clicking on the download option as highlighted in the below media over where you need to download ‘.dmg’ one and carry on with the download procedure.
Tip: You can also opt to download the x64 compressed archive and then extract it to your Mac.But in mac while downloading any application it is suggested to download complete files instead of compressed files due to internal constraints of macOS ocosystem.
Step 2: Open the DMG file -> double click the JDK package to downloaded in the previous step to start installing Oracle JDK 17 on your Mac. It will install the DMG disk and presents the installer as shown in Figure. Go through the updated License Agreement of Oracle JDK.
Step 3: Click the Continue button -> select destination -> install -> Enter your password to give permission to install java 17.
Note: You may change the installation location by clicking the Go Back Button. It didn’t allow me to change the installation location on Big Sur.
Step 4: Now unmount the DMG image as shown in Fig by right-clicking it basically we are deleting the setup just unlikely we do in Windows
Step 5: To confirm the installation, we can check the JDK version, as shown in the picture below. That’s all about installing Oracle JDK 17 on macOS.
How to set/change the Path of Java on macOS?
We need to be super attentive and careful as it is very useful in understanding java on any machine. Now we need to change the path of Java on Mac, for which certain steps are to be followed these steps :
Step 1: Open Terminal -> type: “which java”. It should show something like /usr/bin/java.
JAVA_HOME is the full path of the directory that contains a sub-directory named ‘bin’ which includes the JavaJava. For Mac OSX – it is /Library/Java/Home
Step 2: Set JAVA_HOME using this command in Terminal, you no longer need to remember just stress over it and go on
Step 3: To confirm the path, write the following command
Step 4: You should now be able to run your application
Note: This only sets JAVA_HOME for this session. If you want it to be permanent, you’ll have to add the command to your ~/.zshenv or ~/.zshrc file as on macOS 10.15 Catalina, and later, the zsh is the default Terminal shell. To accomplish this instead follow these instructions:
Step 5: Open terminal and type:
One can also use any other terminal command line text editors of your choice
Step 6: Adding this to the end of the ~/.zshenv file
Step 7: Save and exit nano (ctrl-x, option-a, enter)
Step 8: Source the file and print the $JAVA_HOME.
source ~/.zshenv echo $JAVA_HOME
If we do see the below command then we have successfully set your JAVA_HOME environment variable