111

When I type require(xlsx) in order to load the package xlsx in R, the following messages is shown:

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

I've also tried to load the rJava package manually but got this error message:

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. 

Do you have any suggestion on how to solve the problem?

5
  • 5
    Try to install rJava ...Read this Commented Jun 29, 2013 at 5:24
  • 8
    Which platform? If Windows then this may be helpful. Commented Jun 29, 2013 at 6:08
  • also, your problem allow you to first saving your .xlsx file to a csv and then using read.csv, rather than trying to read in the .xlsx file Commented Jun 29, 2013 at 7:40
  • 2
    If the last listed answer (16 votes at this point) fixed it for you, please mark the answer as accepted. Commented Mar 25, 2015 at 11:31
  • I ran into this issue too, but diverted it by using Hadley Wickham's readxl package. More discussion about here - cannot install libraryxlsx in r and look for an alternative Commented Jul 6, 2016 at 19:27

10 Answers 10

151

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.

Sign up to request clarification or add additional context in comments.

4 Comments

(+1) I'm not sure if R's message completely points to this as the problem, but it was the case for me! To be sure, you can check. If you are running 64 bit OS, and 64 bit R, run the command line and try java -d64 version. If you have working 64 bit Java, it could be a system path problem.
Did you mean java -d64 -version?
If you're running a Mac with an M1 pro chip, this is a problem whose resolution remains a work in progress as of Dec 2022 : github.com/rstudio/rstudio/issues/9543 (scroll to bottom)
install JAVA 64-bit! Very good, straight to the point. Saved my day!
10

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.

Comments

10

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.

Comments

8

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.

1 Comment

This did work, but how could it be made permanent? This clearly is just a bandaid, a good one nonetheless.
4

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.

Comments

4

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

Comments

1

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

1 Comment

Unfortunately some excel files can only be opened with XLConnect. Context: I work with a lot of excel files from data sources all over the world. My go to packages are readxl for reading and openxlsx for writing
1

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!

Comments

0

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

Comments

0

You can also try the new R package that takes care of Java installation in one command: https://github.com/e-kotov/rJavaEnv

To install the package:

if (!requireNamespace("remotes", quietly = TRUE)) { install.packages("remotes") } remotes::install_github("e-kotov/rJavaEnv") 

To install Java:

rJavaEnv::java_quick_install(version = 21) 

This will download Java 21 (you are free to change the version, if there are any specific requirements for the R package you are using), extract it into cache folder and link it to your current working directory, set the environment variables for Java to work in the current session and save the settings to set this Java in the .Rprofile of the current project/working directory. Read more in the package documentation: http://www.ekotov.pro/rJavaEnv/

P.S. Disclosure: I'm the package author.

P.P.S at the moment this may not work on some Linux and Intel-based macOS systems.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.