rJava install error JAVA_HOME cannot be determined from the Registry
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.
http://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/
R xlsx package can not be used
The xlsx
package depends on the rJava
package, which requires a valid installation of the Java Runtime Environment (JRE). Before installing the rJava
package, one must install the Java Runtime Environment (JRE). The process for installing the JRE varies by operating system, and Oracle has made this process more difficult due to changes they made in the licensing requirements for Java starting in 2019.
How do I tell whether the JRE is installed?
As noted in the comments on the question, one can verify the version of the Java runtime that is installed on a machine with the command line java -version
. It is important to also confirm that the Java runtime is accessible from R / RStudio. We can do this by executing the system()
function within RStudio, also as noted in the comments for the question.
> system("java -version")
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
[1] 0
>
Reading Excel files into R
There are two distinct approaches to solving this problem. Here is a summary of instructions that I wrote on this topic for the Johns Hopkins Data Science Specialization Getting and Cleaning Data course, Common Problems: Java and xlsx package back in 2017.
Solution 1: Use an Excel Reader Package that Doesn't Require Java
PRO TIP: The easiest way to work around this problem is to use an R package that does not depend on Java, such as openxlsx or readxl.
For openxlsx
, it's very easy.
install.packages("openxlsx")
library(openxlsx)
# read the help file to identify the arguments needed to
# correctly read the file
?openxlsx
theData <- read.xlsx(...)
The same process can be used for readxl
.
install.packages("readxl")
library(readxl)
# read the help file to identify the arguments needed to
# correctly read the file
?readxl
theData <- read_excel(...)
Note that there is a complementary package, writexl
, that enables people to write Excel files.
Solution 2: Install Java and Required R Packages
That said, for people who want to use the xlsx
package to work with Excel files, there are workable solutions for Windows, Mac OSX, and Ubuntu Linux.
SOLUTION (Windows): Download and install the latest version of the Java Runtime Environment from Oracle. Note that if you are running the 64-bit version of R, you need to install the 64-bit version of the Java Runtime.
SOLUTION (Mac OSX): As of newer releases of Mac OSX, this has become more complicated. A specific set of commands needs to be followed after installing the Java Development Kit on the computer. These are documented on the rJava Issue 86 github page. I have included a screenshot of this solution for people to reference directly.
SOLUTION (Ubuntu): Use the Ubuntu Advanced Packaging Tool to install Java, then reconfigure Java in R.
sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
sudo R CMD javareconf
Then in R / RStudio install the xlsx
package.
install.packages("xlsx")
32-bit vs. 64-bit Java in Windows
Another common problem people may encounter is an incompatibility between the version of the Java Runtime Environment that is installed on their computer and the version of R, either 32-bit or 64-bit.
For example, if one has installed the 64-bit version of R but has the 32-bit version of Java Runtime Environment installed, R will not have visibility to the Java Runtime Environment, generating the same "Java not installed error" as noted above.
SOLUTION: This problem can be resolved by either installing the 64-bit version of Java Runtime for Windows, or by changing the RStudio configuration to use the 32-bit version of R.
Related Topics
What Does Java.Lang.Thread.Interrupt() Do
How to Pass an Array as Arguments to a Method With Variable Arguments in Java
Hiding Strings in Obfuscated Code
Limit Decimal Places in Android Edittext
Using Singleton Design Pattern for SQLitedatabase
How to Delete Firebase Data After "N" Days
Setting/Changing the Ctime or "Change Time" Attribute on a File
Why Stringbuilder Stops Adding Elements After Using the Null Character
Java I/O VS. Java New I/O (Nio) with Linux Nptl
How to Solve the 'Lock Obtain Timed Out' When Using Solr Plainly
Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used
Load Native Libraries in an Eclipse Rcp Application on Linux
Invoking Bash from Java Interactively