R looking for the wrong java version
You problem depends on 64/32 bit versions.
You run 32-bit R, which use 32-bit command prompt and find 32-bit java. If you use 64-bit R then it runs 64-bit command promt and proper java.
You could check it by run 32-bit command promt (following this post):
- Click Start.
- Type
%windir%\SysWoW64\cmd.exe
in Start Search box. - Press Enter.
- Type
java -version
In my system it fails because I don't have 32-bit java. With standard cmd.exe I get proper path.
For possible solution there are two ways. Install 32-bit R and 32-bit Java or 64-bit R (which is officially supported from 2.11 version) and 64-bit Java. On my system (64-bit Windows 7) I've got both sets, so on 32-bit combination I get:
> system("java -version")
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)
And on 64-bit:
> system("java -version")
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) 64-Bit Server VM (build 16.0-b13, mixed mode)
On 64-bit version you could call 32-bit Java using 32-bit cmd:
shell(
"java -version",
shell = file.path(Sys.getenv("windir"),"SysWoW64/cmd.exe")
)
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)
About Shane's comment I think the question is how R get path to 32-bit cmd. Because I can't find a way to call 64-bit cmd on 32-bit R.
configuration failed for package ‘rJava’ when running rize - R
It looks like you have issues with setting proper location of Java. Note that making R
happy with Java
might be a little bit of struggle.
Take a look here: http://www.owsiak.org/r-java-11-and-making-sure-you-can-load-rjava/ for a sample. It is macOS
based, but still, it should work fine.
Basically, what you want to do is to set your JAVA_HOME
such way it points at Java
installation. Once it's done, you can run R cmd javareconf
like this
> sudo R CMD javareconf \
JAVA_HOME=${JAVA_HOME} \
JAVA=${JAVA_HOME}/bin/java \
JAVAC=${JAVA_HOME}/bin/javac \
JAVAH=${JAVA_HOME}/bin/javah \
JAR=${JAVA_HOME}/bin/jar
Make sure you have gcc
installed and that you can compile simple C
code.
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.
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/
Related Topics
Create Polygon from Set of Points Distributed
Creating a Sankey Diagram Using Networkd3 Package in R
Detecting Cycle Maxima (Peaks) in Noisy Time Series (In R)
Provide Shades Between Dates on X Axis
Error Installing Packages from Github
How Can Library() Accept Both Quoted and Unquoted Strings
Combined Line & Bar Geoms: How to Generate Proper Legend
Frequency Table with Several Variables in R
How to Summarizing Data Statistics Using R
Splitting Columns by Number of Characters
A Way to Access Google Streetview from R
Questions About Set.Seed() in R
Creating a Function in R with Variable Number of Arguments,
Warning When Defining Factor: Duplicated Levels in Factors Are Deprecated
Percentage Histogram with Facet_Wrap