Java Development on a Mac – DigitalBytes from AssetBytes
Setting up or Updating Java on a Mac machine is a lot different than installing Java on a Windows machine. Here’s the steps one of our developers had to follow to set up their Java development environment on a Mac….

So you have a new Mac (or an old Mac) and you want to do some Java development on your Mac. The first thing you need to do is check to see if you have Java already installed on your machine and if installed what’s the Java version installed. So you open your Terminal window and type in

java -version

Depending on if you have Java installed or not you’ll either get

Java not installed

or information about the actual Java version installed, something akin to

java version “1.8.0_144”
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

In either case you want to now install Java or upgrade to the latest/newer version, so the first thing you do is go to Google of course and search for ‘how to install Java on a Mac‘ and you get tons of results with the primary one being

You could follow the instructions there-in and go along your merry way but if you want the flexibility and control of having multiple versions of Java on your mac and being able to switch versions and being able to switch between the Oracle version of Java and the OpenJDK version of Java then you are better off using our favorite HomeBrew package manager for MacOS to install Java. If you don’t have brew already installed, follow the instructions on the HomeBrew page to install brew first. If you already have brew you installed you are more likely to just go back to your terminal shell and type in

brew cask install java

That used to work but as off Aug 2020 you will more than likely get the following error..

Error: Cask ‘java’ is unavailable: No Cask with this name exists.

That’s cause you now need to tell Brew which flavor of the Java JDK you want installed (and which version) on your Mac. We at AssetBytes prefer the OpenJDK version of Java so at your terminal prompt again do the following

brew tap adoptopenjdk/openjdk
brew search jdk

The output of the search command should give you a listing of all the JDK flavors available to you to install, so you can choose one or many of the specific versions of Java to install. You typically need only the latest but you might need an older version for compatibility with an existing app/program on your Mac. If you already had for example Oracle JDK 1.8 installed as mentioned above, you can install the latest Java 15 (as of this writing) version of OpenJDK using the command

brew cask instal adoptopenjdk15

Once that’s installed, you now need to add aliases and set up your JAVA_HOME environment to be able to switch between the different versions of Java installed on your machine. So first execute the command

/usr/libexec/java_home -V

to determine the Java versions installed, then add the following aliases to your login profile (.zshrc or .bashrc) file

export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8)
export JAVA_HOME_15=$(/usr/libexec/java_home -v15)

The above commands add a JAVA_HOME path to Java 1.8 and Java 15 and set the version in use to Java 15. Once you add the above aliases, start a new terminal or source the .zshrc or .bashrc file to pick up the changes and now you can verify you have new/latest version of Java installed and configured by executing from your terminal

java -version

again and you should see an output like..

openjdk version “15” 2020-09-15
OpenJDK Runtime Environment AdoptOpenJDK (build 15+36)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15+36, mixed mode, sharing)

Note: If /usr/libexec/java_home gives you an error stating Java not found though you know Java is installed cause java -version shows you the right version of openjdk then do the following. Run

brew info openjdk

That should output information about the OpenJDK keg installed by brew and should include information on how to symlink it for external wrappers (like invoked by /usr/libexec/java_home) so create the link using the information printed, something along the lines of…

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x