Rjava could not be loaded

·

3 min read

I'll be enlightening you on a more detailed note on how to replicate the error message "JAVA_HOME cannot be determined from the Registry" that leads to 'rJava' could not be loaded'. This error message normally appears when we want to load the xlsx package as shown below:

-

install.packages("xlsx")           # Install & load xlsx
library("xlsx")
# Loading required package: xlsx
# Loading required package: xlsxjars
# Loading required package: rJava
# Error : .onLoad failed in loadNamespace() for 'rJava', details:
#   call: fun(libname, pkgname)
#   error: JAVA_HOME cannot be determined from the Registry
# Failed with error:  'package 'rJava' could not be loaded'

After installing and loading the xlsx package, the error "JAVA_HOME cannot be determined from the Registry" was returned to the RStudio console. This difficulty, however, is caused by the rJava package, rather than the xlsx package. When installing the xlsx package, R also tries to install the rJava package. So why does the error "JAVA_HOME cannot be determined from the Registry" appear when trying to install the rJava package? This is usually because you are working on a 64-bit version of R but not a 64-bit version of Java. You may have 32-bit version of Java installed on your system, or you might not have installed Java at all.

- Note that the Java download page recommends the 32-bit version by default. Hence, it is necessary to install the 64-bit version manually. Conclusively speaking, you get the error 'rJava could not be loaded' when you're trying to load a package that relies on the {rJava} package. The issue indicates that there is no entry in the Registry that informs R of the location of Java. As a result, it's very likely that Java wasn't installed (or the Registry is corrupt). Installing a Java version (64-bit Java or 32-bit Java) that matches the sort of R version you're using usually fixes the problem (i.e., 64-bit R or 32-bit R). T his problem can easily affect Windows 7 users, because they may have installed a version of Java that differs from the version of R they are using You can install both versions of Java if you need to work on both versions of R for some reason (Installing the "Java Runtime Environment" is probably good enough for your needs).

-

Other possible solutions

Several environment variables and paths need to be set on your system for the 'rJava' package to load successfully. You should verify the following on Windows:

Ensure that the environment variable 'JAVA_HOME' is set properly and points to a 64-bit version of Java(JDK or SE Runtime). For 64-bit Revolution R, you need to ensure that you have a 64-bit version of Java installed.

-

Ensure that the Java 'CLASSPATH' variable is set accurately. For the 64-bit version of the JDK, this is normally set to something like the following (on Windows):

'C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext'

Look into your system PATH settings. On Windows, goto 'Control Panel ---> System --> Advanced Settings ---> Environment Variables'. Your path should include the directories:

'C:\Program Files\Java\jre6\bin\server' and
'C:\Revolution\R-Enterprise-7.3\R-3.1.1\bin\x64'.

Ensure that the environment variables 'RPATH' and 'RHOME' are BOTH set and point to the right directory:

'C:\Revolution\R-Enterprise-7.3\R-3.1.1\bin\x64'

The same settings are required on Linux, but the paths will be different:

CLASSPATH:  /usr/java/jdk1.7.0_02/jre/lib/ext
RPATH and RHOME:  /usr/lib64/Revo-7.3/R-3.1.1

On Linux, it is also suggested that you run the command: R CMD javareconf

- This will attempt to properly configure Java on your system in order to run Revolution R Enterprise.

.

Conclusion

Note that if you don't fill out ALL of the configuration parameters after running 'R CMD javareconf,' it's likely that rJava won't work and your Java installation/configuration is considered inadequate. Happy Troubleshooting :)