How to load the {rJava} package after the error "JAVA_HOME cannot be determined from the Registry"

In case you tried loading a package that depends on the {rJava} package (by Simon Urbanek), you might came across the following error:

Loading required package: rJava
library(rJava)
Error : .onLoad failed in loadNamespace() for ‘rJava’, details:
call: fun(libname, pkgname)
error: JAVA_HOME cannot be determined from the Registry

The error tells us that there is no entry in the Registry that tells R where Java is located. It is most likely that Java was not installed (or that the registry is corrupt).

This error is often resolved by installing a Java version (i.e. 64-bit Java or 32-bit Java) that fits to the type of R version that you are using (i.e. 64-bit R or 32-bit R). This problem can easily effect Windows 7 users, since they might have installed a version of Java that is different than the version of R they are using.

Note that it is necessary to ‘manually download and install’ the 64 bit version of JAVA. By default, the download page gives a 32 bit version .

You can pick the exact version of Java you wish to install from this link. If you might (for some reason) work on both versions of R, you can install both version of Java (Installing the “Java Runtime Environment” is probably good enough for your needs).
(Source: Uwe Ligges)

Other possible solutions is trying to re-install rJava.

If that doesn’t work, you could also manually set the directory of your Java location by setting it before loading the library:

Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre7') # for 64-bit version
Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre7') # for 32-bit version
library(rJava)

(Source: “nograpes” from Stackoverflow, which also describes the find.java in the rJava:::.onLoad function)

28 thoughts on “How to load the {rJava} package after the error "JAVA_HOME cannot be determined from the Registry"”

  1. Thank you, thank you, this was very helpful. (Needed to manually install 64 bit java for my script to work on a coworker’s windows machine) :)

  2. This was really helpful, thanks! When running from the command line I found it important to specific whether to run from /bin or /bin/x64. If Java is 64 bit, use the latter.

  3. > Sys.setenv(JAVA_HOME=’C:Program FilesJavajre7′) # for 64-bit version
    > Sys.setenv(JAVA_HOME=’C:Program Files (x86)Javajre7′) # for 32-bit version
    > library(rJava)

    Error : .onLoad failed in loadNamespace() for ‘rJava’, details:
    call: inDL(x, as.logical(local), as.logical(now), …)
    error: unable to load shared object ‘C:/Users/ryusukekenji/Documents/R/win-library/2.15/rJava/libs/x64/rJava.dll':
    LoadLibrary failure: %1 is not a valid Win32 application.

    I am using 64bit windows, tried to install 64bit Java and also 32bit Java but keep giong error… May I know how to solve this problem?

    1. Thank Kine,
      You are of course correct.
      The code was modified without my knowledge due to a recent change I made to the blog.
      I fixed the code now to include “\\”

      (p.s.: I checked an old cache of this page, and in it indeed the code was as it should be
      http://web.archive.org/web/20140413084730/http://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/
      )

      Thanks again,
      Tal

  4. Thank you so much! Finally someone who can explain a complicated-seeming topic in a very comprehensive manner – I´ve been trying to “uncode” the things that were written about changing directories and stuff on other pages but never was successful – now it worked out in 5 minutes! You save my day 😉

Leave a Reply