Because programmers can’t get enough caffeine ;)
Overview
Like a long tour with commentary by an experienced guide, this is a deep dive into the various editions to equip you to debug JVM installation issues. “PROTIP” mark little-know or important facts not seen in many other tutorials.
There are several ways to install Java.
Which Java You Got?
You’ll come back to this after installation.
-
Open a Terminal session.
-
Shell scripts check if Java is available by:
command -v java
Alternately, the older form is:
which java
Either way, the response:
/usr/bin/java
PROTIP: “java” in the above path is a binary file.
-
List which versions are installed on your machine by looking at where MacOS installs programs:
ls -al /Library/Java/JavaVirtualMachines/
NOTE: This directory is at the root for the whole machine, not a particular user’s home folder.
The response on my machine, after all versions of Java are installed (at time of writing):
total 0 drwxr-xr-x 6 root wheel 192 Jul 24 11:13 . drwxr-xr-x 5 root wheel 160 Nov 18 2018 .. drwxr-xr-x 3 root wheel 96 Mar 30 2018 jdk1.8.0_162.jdk drwxr-xr-x 3 root wheel 96 Feb 9 2019 jdk1.8.0_202.jdk drwxr-xr-x 3 root wheel 96 Jul 24 11:13 zulu-16.jdk drwxr-xr-x 18 root wheel 576 Nov 18 2018 zulu8.30.0.1-jdk8.0.172-macosx_x64
WARNING: Older versions may not contain the latest security patches.
-
PROTIP: The Java program looks for the $JAVA_HOME environment variable to obtain files:
echo $JAVA_HOME
Sample response:
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
PROTIP: This command is my preferred way to see what a machine has because if you invoke java or javac, if it’s not installed, MacOS prompts you to install the JDK. Clever. But don’t do it if you want other version of Java.
Java is used by Groovy, Grails, Spring Boot, and many others.
-
Additional details are provided by this command:
/usr/libexec/java_home -V
That’s a capital -V.
The response on my machine:
Matching Java Virtual Machines (1): 1.8.0_232, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Alternately:
Matching Java Virtual Machines (4): 1.8.0_45, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home 1.7.0_65, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_65.jdk/Contents/Home 1.6.0_65-b14-466.1, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 1.6.0_65-b14-466.1, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
-
PROTIP: It’s kinda ironic, but to see what Java VM you have already installed, it helps if you know what version you have.
Java 8 and before uses a non-standard parameter. Most other programs use either the -v flag or two-dash --version with the longer-form parameter name. However, with Java:
java -version
A sample response:
java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
Alternately, Java 9 and after uses the standard “–version” (with two dashes) like most other Java programs:
java --version
-
PROTIP: Java developers use the JDK rather than the JRE (Runtime Environment) in order to get the javac compiler. See what version of the Java Compiler is installed:
javac -version
A sample response:
javac 1.8.0_162
I’ll stay with version 8 then
Like many other open source advocates, we continue to use version 8 even though more recent versions have been created by Oracle.
This is because Oracle changed their licensing after version 8 in 2017.
PROTIP: Many prefer to use Java 8 because it is the last version which is free open-source licensed.
PROTIP: Hold off downloading the java .dmg installer file as described at https://java.com/en/download/help/mac_install.xml
WARNING: Oracle installs an annoying Ask Toolbar, without asking.
Oracle’s versions are described at http://www.oracle.com/technetwork/java/javase/downloads/index.html
Oracles docs on installing the JDK:
https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.htmlNOTE: Downloads of the JDK (Java Development Kit) contains the JRE (Java Runtime Engine).
-
PROTIP: Install not the latest 64-bit Java Development Kit (JDK), but the last stable v1.8 version:
brew tap caskroom/versions brew install --cask java8
The response:
==> Caveats This Cask makes minor modifications to the JRE to prevent issues with packaged applications, as discussed here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=411361 If your Java application still asks for JRE installation, you might need to reboot or logout/login. Installing this Cask means you have AGREED to the Oracle Binary Code License Agreement for Java SE at https://www.oracle.com/technetwork/java/javase/terms/license/index.html ==> Satisfying dependencies ==> Downloading http://download.oracle.com/otn-pub/java/jdk/8u152-b16/aa0333dd30 ######################################################################## 100.0% ==> Verifying checksum for Cask java8 ==> Installing Cask java8 ==> Running installer for java8; your password may be necessary. ==> Package installers may write to any location; options such as --appdir are i Password: ==> installer: Package name is JDK 8 Update 152 ==> installer: Installing at base path / ==> installer: The install was successful. 🍺 java8 was successfully installed!
Apple Java obsoleted
PROTIP: The version that comes installed on Apple Macs is obsolete and thus does not have the latest security patches. But do NOT delete the default version.
But if you did uninstall it, re-install Java 6 for OS X 2014-001 obtained from https://support.apple.com/kb/DL1572?locale=en_US
Different Javas available
https://www.wikiwand.com/en/List_of_Java_virtual_machines lists all known Java compilers.
Several organizations work on OpenSDK specs from AdoptOpenJDK and certified for Java SE TCK compliance on x64 reference architecture systems
-
When Oracle acquired Sun, the jdk (Java Development Kit) was one of the products obtained. Versions are downloaded directly from Oracle from http://jdk.java.net.
-
https://adoptopenjdk.net/ has both 8, 11, and 13 running the tradition “HotSpot” JVM and the more recent and faster “OpenJ9” JVM. To install the latest (v13):
brew install --cask adoptopenjdk
Zulu
Azul Zulu downloads for macOS, from a company that also sells Java optimization products. Its zulu8.30.0.1-jdk8.0.172-macosx_x64 from zip April 18, 2018 is 179.2 MB expanded
brew search zulu
The response:
==> Formulae zurl ==> Casks zulu zulu11 zulu13 zulu15 zulu7 zulu8 zulufx
-
-
To install the latest:
brew install zulu
The response:
==> Downloading https://cdn.azul.com/zulu/bin/zulu16.30.15-ca-jdk16.0.1-macosx_x ######################################################################## 100.0% ==> Installing Cask zulu ==> Running installer for zulu; your password may be necessary. Package installers may write to any location; options such as `--appdir` are ignored. installer: Package name is Zulu 16.30+15 installer: Installing at base path / installer: The install was successful.
Install Jenv to manage multiple Versions of Java
PROTIP: Much like NPM for NodeJs developers and rbenv for Ruby, if you’re a developer, you’ll likely need to manage different versions of Java needed by different apps.
http://hanxue-it.blogspot.com/2014/05/installing-java-8-managing-multiple.html?q=java
describes the steps.
-
Consider the Jenv utility:
brew info jenv
The response at time of writing:
jenv: stable 0.5.4 (bottled), HEAD Manage your Java environment https://www.jenv.be/ Not installed From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/jenv.rb License: MIT ==> Options --HEAD Install HEAD version ==> Caveats To activate jenv, add the following to your /Users/wilsonmar/.bash_profile: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" ==> Analytics install: 9,034 (30 days), 28,364 (90 days), 124,693 (365 days) install-on-request: 9,030 (30 days), 28,348 (90 days), 124,090 (365 days) build-error: 0 (30 days)
-
Install Jenv by specifying the location URL: https://raw.githubusercontent.com/entrypass/jenv/homebrew/homebrew/jenv.rb
brew install jenv
The response:
... ==> Caveats To activate jenv, add the following to your /Users/wilsonmar/.bash_profile: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" ==> Summary 🍺 /usr/local/Cellar/jenv/0.5.4: 84 files, 73KB
Previously:
🍺 /usr/local/Cellar/jenv/0.4.4: 78 files, 65.5KB, built in 8 seconds
-
Instead of editing ~/.bash_profile as described above, paste this which handles errors better:
export JENV_ROOT="$(which jenv)" # /usr/local/var/jenv if command -v jyenv 1>/dev/null 2>&1; then eval "$(jenv init -)" fi
-
Restart:
source ~/.bash_profile
-
To see if jenv can run, list its version and commands:
jenv
The response is like this (at time of writing July 24, 2021):
jenv 0.5.4 Usage: jenv <command> [<args>] Some useful jenv commands are: commands List all available jenv commands local Set or show the local application-specific Java version global Set or show the global Java version shell Set or show the shell-specific Java version rehash Rehash jenv shims (run this after installing executables) version Show the current Java version and its origin versions List all Java versions available to jenv which Display the full path to an executable whence List all Java versions that contain the given executable add Add JDK into jenv. A alias name will be generated by parsing "java -version" See `jenv help
' for information on a specific command. For full documentation, see: https://github.com/hikage/jenv#readme -
See where the symlink leads:
ls -al $(which jenv)
My response (at time of writing):
lrwxr-xr-x 1 wilsonmar admin 29 Jul 24 10:10 /usr/local/bin/jenv -> ../Cellar/jenv/0.5.4/bin/jenv
NOTE: The file jenv is a binary executable.
-
Use Homebrew’s directories rather than ~/.jenv add to the bottom of your bash_profile file:
export JENV_ROOT=”$(which jenv)” echo “$JENV_ROOT”
Jenv for several Java versions
-
Get info:
jenv info java
Sample response:
Jenv will exec : /usr/bin/java Exported variables : JAVA_HOME=/usr/local/bin/jenv/versions/system
-
List installers available for use by jenv:
ls -al /Library/Java/JavaVirtualMachines/
The response:
total 0 drwxr-xr-x 6 root wheel 192 Jul 24 11:13 . drwxr-xr-x 5 root wheel 160 Nov 18 2018 .. drwxr-xr-x 3 root wheel 96 Mar 30 2018 jdk1.8.0_162.jdk drwxr-xr-x 3 root wheel 96 Feb 9 2019 jdk1.8.0_202.jdk drwxr-xr-x 3 root wheel 96 Jul 24 11:13 zulu-16.jdk drwxr-xr-x 18 root wheel 576 Nov 18 2018 zulu8.30.0.1-jdk8.0.172-macosx_x64
The folders above provide version handles (such as “zulu-16.jdk”) for jenv to reference.
For each version, along with lib and man (manual) folders, Jenv looks into a bin folder containing executables to use.
-
Construct the a path to those folder for a version by adding folder /Contents/Home to the known path:
FOLDER="/Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home" ls "$FOLDER"
The response:
DISCLAIMER bin demo jmods lib readme.txt Welcome.html conf include legal man release
Add Java versions
-
Construct a command to the path by adding”/Contents/Home” to the path you already know, such as this:
jenv add "$FOLDER"
A sample response:
zulu-16.jdk added
-
List the Java versions jenv knows about:
jenv versions
The response if none if brew cask was not installed:
* system (set by /usr/local/var/jenv/version)
-
To configure global version for all apps to use:
jenv global oracle64-1.8.0.45
Example response:
system oracle64-1.7.0.65 * oracle64-1.8.0.45 (set by /Users/wilsonmar/.jenv/version)
Open JDK
Open JDK is the open-sourced implementation of the JRE spec, at http://openjdk.java.net
But some say it is not ready for “prime time” on MacOS because its installation is a dirty affair from 2013 when going from v6 to v7:
- https://wiki.openjdk.java.net/display/MacOSXPort/Mac+OS+X+Port+Project+Status
Open JDK’s install page at http://openjdk.java.net/install/ shows apt-get (for Debian, Ubuntu) and yum (for Red Hat, CentOS, Oracle Linus, Fedora).
-
http://blog.shelan.org/2015/03/how-to-build-open-jdk-9-on-mac-osx.html on Yosemite
-
http://hanxue-it.blogspot.com/2014/05/installing-java-8-managing-multiple.html
For macOS, this page recommends using Make to compile from source.
-
Download source using Mercurial to a new folder “openjdk9”:
hg clone http://hg.openjdk.java.net/jdk9/jdk9 openjdk9 cd ./openjdk9
http://hg.openjdk.java.net/jdk8/jdk8 work stopped at 2014-03-04.
-
Install XQuartz for X Window System that runs on Macs:
brew install Caskroom/cask/xquartz
brew cask listNOTE: Downloaded /Library/Caches/Homebrew/xquartz-2.7.9.dmg was 7.9K
xquartz staged at ‘/opt/homebrew-cask/Caskroom/xquartz/2.7.9’ (73M)
- cd to the .tar.gz directory containing the configure file for Make to use.
-
XQuartz fixes an error in this command:
bash ./configure
-
Install apple-gcc42 the Apple C compiler.
-
Install ccache (compiler cache):
brew install ccache
- Make a symlink /usr/bin/gcc -> /usr/local/Cellar/apple-gcc42/4.2.1-5666.3/bin/gcc-4.2 (same for g++)
Test JDK Build
https://mihail.stoynov.com/2015/01/29/building-openjdk-9-on-a-osx-or-any-linux/ shows JDK 9 install using ccache, mercurial (hg), jtreg “testing harness”, and Webrev to diff code
Update Java
https://java.com/en/download/help/mac_java_update.xml
NOTE: Others include cheatsheet, google-chrome, google-drive, google-hangouts, dropbox, etc. listed in Sourabh Bajaj’s venerable Mac OSX Setup Guide, developed and distributed as a GitBook.
Maven (mvn)
-
Install using Homebrew:
brew install maven
-
See its metadata and what java version Maven is based on:
mvn -version
If it’s installed, the sample response:
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d) Maven home: /usr/local/Cellar/maven/3.8.1/libexec Java version: 1.8.0_162, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre Default locale: en_US, platform encoding: utf-8 OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
PROTIP: Many have switched to using Gradle instead of Maven or Ant.
Kotlin
Kotlin is a language that makes use of the JVM. Invented by JetBrains (IntelliJ).
Social
Top Java Blogs/bloggers to follow on Twitter:
- @InfoQ
- @baeldung
- @javarevisited
- @AdamBien
- @vlad_mihalcea
- @JavaOOQ
- @thjanssen123
- @The_Java_Dev
- @JournalDev
- @nicolas_frankel
- @starbuxman
- @DZone
- @GuidesJava
- @SaralSaxena
- @rafaelcodes
- @arungupta
- @javinpaul
- @JavaWorldCom
More on macOS
This is one of a series about macOS (previously Mac OSX):
- MacOS Setup step-by-step, with automation
- MacOS Hardware and accessories
- MacOS dotfiles for System Preferences setup automation
- MacOS Boot-up
- MacOS Keyboard tricks
- MacOS Terminal Tips and Tricks
- Text editors and IDEs on MacOS
- MacOS Xcode.app and CommandTools (gcc)
- MacOS Command-line utilities
- Applications on MacOS
- 1password on MacOS
- Manage Disk Space on MacOS
- Screen capture on MacOS
- MacOS iPhone integration
- Linux and Windows on Apple MacOS
- Packer create Vagrant Windows image
- Python on MacOS
- Maven on MacOS
- Ruby on MacOS
- Node on MacOS installation
- Java on MacOS
- Scala ecosystem