A few of the replies before this have what may work for you. Some tools need the environment variable JAVA\_HOME set. If your ~/.bashprofile or equivalent is where your JAVA\_HOME
is set, for example:
export JAVAHOME=/usr/libexec/java_home -v 1.8
Then you may want to edit your environment file to use your preferred java version which you can find using
/usr/libexec/javahome -V
Then edit ~/.bashprofile for that version:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/<your java version>/Contents/Home
Note that the terms of use for the Oracle java have changed if you use java professionally, and
there are options that they offer if so - a commercially licensed version through them or that
you can download openjdk instead.