On Mac OS X, how do I install and use Java 8 if I already have a different version of Java installed?

I have Mac OS X. But I already installed Java 10! How can I install Java 8 and make hail work?

This is the error message I’m getting!

py4j.protocol.Py4JJavaError: An error occurred while calling z:is.hail.HailContext.apply.
: is.hail.utils.HailException: Hail requires Java 8, found 10.0.1
	at is.hail.utils.ErrorHandling$class.fatal(ErrorHandling.scala:9)
	at is.hail.utils.package$.fatal(package.scala:26)
	at is.hail.HailContext$.apply(HailContext.scala:221)
	at is.hail.HailContext.apply(HailContext.scala)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
	at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
	at py4j.Gateway.invoke(Gateway.java:282)
	at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
	at py4j.commands.CallCommand.execute(CallCommand.java:79)
	at py4j.GatewayConnection.run(GatewayConnection.java:238)
	at java.base/java.lang.Thread.run(Thread.java:844)

Not a problem at all! You can have two versions of Java installed at the same time.

Download JDK 8 for Mac OS X from Oracle’s website and install it normally.

Whenever you want to use hail, ensure that you first execute this (you must always run this at least once in every terminal in which you will execute python, ipython, or jupyter notebook):

export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"

You can set the default Java version to Java 8 by editing your ~/.profile:

printf "export JAVA_HOME=\"$(/usr/libexec/java_home -v 1.8)\"\n" >>~/.profile

Thanks, this was helpful. I’ve discovered its also possible to set the JAVA_HOME environment variable in a Jupyter notebook cell:

import subprocess
java_home = subprocess.getoutput(["/usr/libexec/java_home", "-v", "1.8"]).strip()
%env JAVA_HOME = $java_home

before initialising Hail.