Recently I was trying to setup a development environment on my Mac and faced a lot of hassle installing Java and setting up the JAVA_HOME. This post is for those who struggle out there.
Installing Java
Select the desired Java version and Download from here.
I prefer JDK8u231 and downloaded the jdk-8u231-macosx-x64.dmg.
Install the .dmg file following the recommended settings. Mac doesn't allow to choose the destination folder to install Java.
Installing the jdk-8u231-macosx-x64.dmg will install JDK (Java Development Kit) and JRE (Java Runtime Environment) on the Mac.
Setting up JAVA_HOME
Latest versions of Mac recommends to use Z Shell zsh (advanced version of bash).
In June 2019, Apple announced that the forthcoming macOS Catalina (10.15) would adopt Zsh as the default shell, replacing Bash. Refer here for more information on Z Shell.
In order to use Java commands from the zsh terminal we need to set the JAVA_HOME in the .zshrc file.
Following are the locations of Java JDK and JRE installations:
JDK Location : /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk
JRE Location : /Library/Internet\Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Open the .zshrc file from any editor and add the following lines to export the JAVA_HOME.
That is it. We have setup the JAVA_HOME.
Remember you cannot set the JAVA_HOME to the JRE location.
Check whether the JAVA_HOME is set properly :
>>echo $JAVA_HOME
If the JAVA_HOME is set in the system the above command will print the path.
Check the Java version :
>>java -version
To print the JDK installation location in the system :
>>which java
On Mac this will show the /usr/bin/java which is a symbolic link to the actual JDK installation location.
We can use the following command to unravel the exact directory path of the JDK location :
>>$(dirname $(readlink $(which javac)))/java_home
Alternatively we can use Java to find the JAVA_HOME in a platform-independent way.
The command below will give several java properties one of them will be the java.home.
>>java -XshowSettings:properties -version
Installing Maven
One other important tool for development is Maven. Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
There are multiple ways we can install Maven, like directly downloading the Maven from the official apache site, using a packager manager like HomeBrew to install Maven on the system for you.
Advantage of using a package manager like HomeBrew is, it will take care of setting up all the environmental variables and time-to-time automatically update to the newer versions.
Installing HomeBrew on Mac
Execute the following command in the terminal :
>>/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Find more details on the site
Installing Maven using HomeBrew
To install Maven using HomeBrew package manager execute the following :
>>brew install maven
Find Maven version and Location :
>>mvn --version
That concludes our objective for the day!
Thank you for reading...
Please leave your constructive comments for improvement!
Comments