Because programmers can’t get enough caffeine
There are several ways to install Java. This is a deep dive into the various editions to equip you to debug JVM installation issues.
Which Java You Got?
Open a Terminal session.
Shell scripts check if Java is available by:
command -v java
Alternately, the older form is:
Either way, the response:
PROTIP: File “java” in the above path is a binary file.
To see what Java VM you have already installed:
PROTIP: -version is a non-standard parameter. Most other programs use either the -v flag or two-dash --version with the longer-form parameter name.
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)
List which versions are installed on your machine:
PROTIP: On Macs, all known JVM’s are located at:
NOTE: This directory is at the root for the whole machine, not a particular user’s home folder.
The response on my machine:
adoptopenjdk-8.jdk jdk1.8.0_162.jdk jdk1.8.0_202.jdk zulu220.127.116.11-jdk8.0.172-macosx_x64
PROTIP: The Java program looks for the $JAVA_HOME environment variable to obtain files:
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 others.
PROTIP: 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:
A sample response:
Additional details are provided with this command:
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
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: 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, to re-install Java 6 for OS X 2014-001, it can be obtained from https://support.apple.com/kb/DL1572?locale=en_US
https://en.wikipedia.org/wiki/List_of_Java_virtual_machines lists all the known Java compilers. Several organizations work on OpenSDK specs from AdoptOpenJDK and certified for Java SE TCK compliance on x64 reference architecture systems
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 cask install adoptopenjdk
- Azul Zulu downloads for macOS, from a company that also sells Java optimization products. Its zulu18.104.22.168-jdk8.0.172-macosx_x64 from zip April 18, 2018 is 179.2 MB expanded
brew cask search zulu
Latest Version of Oracle Java
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.
Oracles docs on installing the JDK:
Use an internet browser to
Click the “Download” button for the JDK (not the JRE).
NOTE: Downloads of the JDK contains the JRE.
Install Jenv to manage multiple Versions of Java
PROTIP: If you’re a developer, you’ll likely need to manage different versions of Java needed by different apps. Much like NPM for Node and rbenv for Ruby.
Install Jenv by specifying the location URL: https://raw.githubusercontent.com/entrypass/jenv/homebrew/homebrew/jenv.rb
brew install jenv
==> Downloading https://github.com/gcuisinier/jenv/archive/0.4.4.tar.gz ==> Downloading from https://codeload.github.com/gcuisinier/jenv/tar.gz/0.4.4 ######################################################################## 100.0% 🍺 /usr/local/Cellar/jenv/0.4.4: 78 files, 65.5KB, built in 8 seconds
To see if jenv can run, list its version and commands:
The response is like this (at time of writing Oct 15, 2018):
jenv 0.4.4 Usage: jenv
[ ] 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 See `jenv help ' for information on a specific command. For full documentation, see: https://github.com/hikage/jenv#readme </pre>
See where it was installed:
NOTE: The file jenv is a binary executable.
Use Homebrew’s directories rather than ~/.jenv add to the bottom of your bash_profile file:
To enable shims and autocompletion add to the botton of your bash_profile file:
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
Jenv for several Java versions
jenv info java
Jenv will exec : /usr/bin/java Exported variables : JAVA_HOME=/Users/wilsonmar/.jenv/versions/system
List installers available for use by jenv:
ls -al /Library/Java/JavaVirtualMachines/
total 0 drwxr-xr-x 5 root wheel 160 Mar 30 2018 . drwxr-xr-x 5 root wheel 160 Mar 30 2018 .. drwxr-xr-x 2 root wheel 64 Mar 30 2018 jdk-10.jdk drwxr-xr-x 2 root wheel 64 Mar 30 2018 jdk1.8.0_144.jdk drwxr-xr-x 3 root wheel 96 Mar 30 2018 jdk1.8.0_162.jdk
If you don’t see any, you need to first download a JVM installer containing folders bin, lib, jre, include, bundle, db, man.
The path to a particular version is constructed by adding “/Contents/Home” to the end of the path.
The point of jenv is to add additional versions, such as back version JDK 7.
jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_65.jdk/Contents/Home
Add JDK 8:
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
The above provide a handle for jenv provide other apps to use.
List the Java versions jenv knows about:
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-22.214.171.124
system oracle64-126.96.36.199 * oracle64-188.8.131.52 (set by /Users/wilsonmar/.jenv/version)
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:
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
Download source using Mercurial to a new folder “openjdk9”:
hg clone http://hg.openjdk.java.net/jdk9/jdk9 openjdk9 cd ./openjdk9
Note JDK 9 is under active development.
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 list
NOTE: 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:
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
Install using Homebrew:
brew install maven
See its metadata and what java version Maven is based on:
If it’s installed, the sample response:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /usr/local/Cellar/maven/3.6.3/libexec Java version: 1.8.0_232, vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.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 is a language that makes use of the JVM. Invented by JetBrains (IntelliJ).
Top Java Blogs/bloggers to follow on Twitter:
More on macOS
This is one of a series about macOS (previously Mac OSX):
- MacOS Hardware and accessories
- MacOS Boot-up
- MacOS Terminal Tips and Tricks
- MacOS Keyboard tricks
- Text editors and IDEs on MacOS
- MacOS Setup automation
- MacOS Homebrew installers
- Manage Disk Space on MacOS
- Data Backups on MacOS
- Ports open
- Applications on MacOS
- Windows on Apple MacOS
- Packer create Vagrant Windows image
- Python on MacOS
- Maven on MacOS
- Ruby on MacOS
- Java on MacOS
- Node on MacOS installation
- PHP on MacOS
- Scala ecosystem