由于 rJava 在 R 中加载包时出现的问题

当我键入 require(xlsx)以加载 R 中的包 xlsx时,会显示以下消息:

> require(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’

我也试过手动加载 rJava包,但是得到了这个错误消息:

require(rJava)
Loading required package: rJava
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: fun(libname, pkgname)
error: No CurrentVersion entry in Software/JavaSoft registry! Try re-installing Java and make sure R and Java have matching architectures.

你对如何解决这个问题有什么建议吗?

200546 次浏览

The reason is probably linked to the fact you are using a 64-bit OS and R version but do not have Java installed with the same architecture. What you have to do is to download Java 64-bit from this page: https://www.java.com/en/download/manual.jsp

After that just try to reload the xlsx package. You shouldn't need to re-start R or if it does not work restart once.

I had a similar issue. It is caused due the dependent package 'rJava'. This problem can be overcome by re-directing the R to use a different JAVA_HOME.

if(Sys.getenv("JAVA_HOME")!=""){
Sys.setenv(JAVA_HOME="")
}
library(rJava)

This worked for me.

Its because either one of the Java versions(32 bit/64 bit) is missing from your computer. Try installing both the Jdks and run the code.
After installing the Jdks open R and type the code

system("java -version")

This will give you the version of Jdk installed. Then try loading the rJava package. This worked for me.

Answer in link resolved my issue.

Before resolution, I tried by adding JAVA_HOME to windows environments. It resolved this error but created another issue. The solution in above link resolves this issue without creating additional issues.

I had a similar problem what worked for me was to set JAVA_HOME. I tired it first in R:

Sys.setenv(JAVA_HOME = "C:/Program Files/Java/jdk1.8.0_101/")

And when it actually worked I set it in

System Properties -> Advanced -> Environment Variables

by adding a new System variable. I then restarted R/RStudio and everything worked.

An alternative package that you can use is readxl. This package doesn't require external dependencies.

If you have this error in RStudio, use Lauren's environmental code above and change your R version to the 32 bit version in Tools, Global Options. There should be both 32bit and 64bit R options if you have a newer version. This will require a restart of R, and limit your memory options. Installing the 64 bit version of the jre won't be required though.

If you have this issue with macOS, there is no easy way here: ( Especially, when you want to use R3.4. I have been there already.

R 3.4, rJava, macOS and even more mess

For R3.3 it's a little bit easier (R3.3 was compiled using different compiler).

R, Java, rJava and macOS adventures

For reading/writing excel files, you can use :

  • readxl package for reading and writexl package for writing
  • openxlsx package for reading and writing

With xlsx and XLConnect (which use rjava) you will face memory errors if you have large files

For me, I simply needed to make sure the Java was 64-bit (to match R).

Here's how I downloaded and installed 64-bit Java:

  1. Go to: https://www.java.com/en/download/manual.jsp
  2. Click on "Windows Offline (64-bit)"
  3. Install it (you may need to find the file that downloaded and double click it, or it may automatically install)
  4. Close and reopen your R/RStudio session
  5. Everything will work as expected!