how to install java JDK on MacOS


recently i had the situation where i needed to help a fellow tech enthusiast set up their Mac environment for java development. The first step is to actually get the darn thing installed, which i will try to explain and walk through below.

intro

first thing is a bit of background to understand how it’s “supposed” to work. there are infininte variations to this but i wanted to do a complete manual install from A-to-Z so i understand what all the installers are doing under the hood. this understanding will also help with troubleshooting java installs for others.

To some, it may be easier to just download an installer, either a dmg or pkg file. but, if you start running into trouble, it will be more difficult to troubleshoot something you don’t understand.

MacOs used to come with a pre-installed version of java, called Apple Java. this doesnt exist anymore, but the /usr/bin/java directory does still exist so that’s kinda confusing. Just know that java is not installed here, nor does your machine come with java built-in.

Also, Mac recently switched from bash to zsh as the default shell. something to do with licensing. we will be placing all our environment variables in .zshrc. Also available is zprofile, zlogin, zshenv we will just use zshrc. These are all ran (“sourced”) at slightly different times, and while all will probably work, we don’t care. Again, we will just use zshrc.

There are many different ways to install java, at the end of the day they all really do the same thing, just hide it/do it for you. Many of the package managers (sdkman, brew) have Java JDK and they like to copy the install files to their special custom locations, which is a bit more confusing to me, but useful for them to help you switch between multiple versions installed in parallel.

most people around me are using java 8 or 11, so i will stay with that here as well. i would love to stay more up-to-date, but i don’t think it matters for the simplistic things i usually do. there are many different versions of java, and many different license types, but for our purposes here that’s not something we care about, i will use oracleJava or openJava.

the java executable is just put in a directory, and then the system env variable “JAVA_HOME” is pointed to it, so the system will use whatever file it is pointed at. so really, there isnt an “install” per se, but just a folder location where you place the files, to which the system is pointed.

to me, the “correct” place to place your java install directory is:

/Library/Java/JavaVirtualMachines

of course, all locations will work as long as you point your env variables to it, but for management purposes i like to keep it here.

list of steps:

  1. download the java you want
  2. copy it to the directory you want
  3. set the java_home variable
  4. set the path variable
  5. check it
  6. stretch goal: setup multiple versions

step 1: download

we can download whatever version we want, and there are many out there now. we are looking for a zipped file to extract, typically a tar.gz in our case. if it gives you the choice of an installer or a zipped/compressed archive, choose the archive.

we can find a nice version of the license-burdened one here: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html, or the openJDK version here: https://jdk.java.net/archive/. the oracle version will require a login, and the openJDK will not. the openJDK is only offered in an archive form, whereas installers are only available for the licensed versions.

step 2: move it

take the file you just downloaded, extract it, and take the resulting folder (probably called “jdk-11.0.2.jdk” or something like that), and move or copy it to the java intstalls directory:

sudo mv ./jdk-11.0.2.jdk /Library/Java/JavaVirtualMachines

step 3: set java_home

run this from your terminal:

echo 'export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home' >> ~/.zshrc

step 4: set path

then, run this from your terminal

echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.zshrc

after step 3 and 4, run the following:

source ~/.zshrc

step 5: test

we can run a few commands to make sure everything looks good:

which java
java -v
javac -v

if the java version shows up, then we are sucessful!

step 6: stretch goal


Leave a Reply

Your email address will not be published. Required fields are marked *