Java 8u45 installation unable to locate a Java runtime to invoke

I've downloaded and installed Java JDK 8u45 for OS X 10.10.1 and after the installation has completed (successfully), java -version produces the following:

Unable to locate a Java Runtime to invoke.

Is this an installation bug with this particular version, or have I forgotten to do something?

enter image description here

me@my-laptop$ /usr/libexec/java_home -V
Matching Java Virtual Machines (0):

Default Java Virtual Machines (1):
    1.8.0_45, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

Solution 1:

That error means Java can't work out which runtime to use. There can be a few reasons for this.

I've had this error in the past because I had more than one copy of Java installed, and none were selected for use in Java Control Panel.

  • Open System Preferences and click on the Java icon this should launch the "Java Control Panel".
  • Then select the Java tab and click "View...", this should bring up the "Java Runtime Environment Settings" window.
  • My guess is that you'll find more than one instance of Java listed here. Tick the box beside the one you want to use and click OK.

This is from a much older version with a different layout but it should look something like this. Multiple copies listed with tick boxes to tell the system which you want to use.

Example of Java Preferences window

Another way to check which runtimes you have installed is with the /usr/libexec/java_home -V command. Ideally it should return something like the following.

Matching Java Virtual Machines (1):
    1.8.0_45, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

Failing that you can check whether you have a $JAVA_HOME environment variable set with echo $JAVA_HOME. There is a lot of advice online saying you should set this variable but if you do that then I believe it ignores the settings in Java Control Panel.