R - Error : .onLoad failed in loadNamespace() for 'rJava'
Asked Answered
W

7

49

While loading rJava package, I receive this error:

Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: inDL(x, as.logical(local), as.logical(now), ...)

error: unable to load shared object 'C:/Users/ankitagarwal5/Documents/R/win-library/3.2/rJava/libs/x64/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

In addition: Warning message:
  package ‘rJava’ was built under R version 3.2.3 
Error: package or namespace load failed for ‘rJava’

I referred various posts on StackOverflow and tried to make changes accordingly, but nothing worked. These are the various steps I followed to resolve the issue:

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

This didn't work.

Then I tried this:

options(java.home="C:\\Program Files\\Java\\jre1.8.0_91\\")
library(rJava)

This didn't work either.

My jvm.dll is at this location in my Windows 7 64 bit OS - C:\Program Files (x86)\Java\jre1.8.0_91\bin\client

I also added C:\Program Files (x86)\Java\jre1.8.0_91\bin\client to my PATH variable in my Environment variables and restarted RStudio and tried to install and load rJava again. That also didn't work.

I would really appreciate if someone could please help me resolve this issue.

Thanks guys!

Wawro answered 9/6, 2016 at 19:58 Comment(5)
Try to run from the shell R CMD javareconf and reinstall rJava. No guarantee of course.Maui
Please don't edit your answer into the question. Instead, you the answer section at the bottom of the page.Gasolier
@Gasolier , made the suggested changes. Thanks .Wawro
similar for OS X: #1349342Istle
Just a heads up to anyone who reads this thread and encounters the problem: Try the code that OP tried (but didn't work for him/her) first. I encountered a similar error that was fixed by running the first example they give. Hope this helps someone out.Branca
W
41

This worked for me -

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.

Note that it is necessary to ‘manually download and install’ the 64 bit version of JAVA. By default, the download page gives a 32 bit version .

You can pick the exact version of Java you wish to install from this link. If you might (for some reason) work on both versions of R, you can install both version of Java (Installing the “Java Runtime Environment” is probably good enough for your needs). (Source: Uwe Ligges)

Other possible solutions is trying to re-install rJava.

If that doesn’t work, you could also manually set the directory of your Java location by setting it before loading the library:

Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre7') # for 64-bit version
Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre7') # for 32-bit version
library(rJava)

Refer this link - http://www.r-bloggers.com/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/

Wawro answered 10/6, 2016 at 5:37 Comment(5)
Worked perfectly for me after hunting. Makes complete sense, wish the debugging message within the rJava package was a bit more pointed in this direction.Stratopause
that fits to the type of R version that you are using How might one easily determine this?Marcello
thanks- same issue when trying to install the tabulizer package, and this worked for me.Suavity
If you want 64bit java, get the offline (64 bit )version when you download java.Whispering
@MrPurple for 32 vs 64 R clarification see https://mcmap.net/q/197042/-how-can-i-know-if-r-is-running-on-64-bits-versus-32Whispering
M
11

I encountered the same problem. Be sure to check the file path for Java. To solve the error I needed to download 64bit Java version. However the following code in R did not work as the folder was incorrect for the latest version for Java:

Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre7') # for 64-bit version

Instead I changed the name to:

Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_144') 

Hope that helps

Musil answered 28/7, 2017 at 11:11 Comment(0)
C
6

I have noticed in different fresh installations of R, RStudio and rJava in WIndows, that what fixes the error is:

  1. Uninstall R
  2. Reinstall R but only selecting one architecture. Example, x64, or x86, but not both. That tends to confuse R or RStudio when they are compiling.

In my case, I fixed the message

Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: inDL(x, as.logical(local), as.logical(now), ...)

error: unable to load shared object 'C:/Users/ankitagarwal5/Documents/R/win-library/3.2/rJava/libs/x64/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Following steps 1 and 2 above.

Cufic answered 19/9, 2018 at 10:23 Comment(0)
B
2

I tried what the other have said to not avail. For me the solution was to eliminate the old folders and shortcuts of R version 32 bit on my computer that remained there even after uninstalling.

Bibb answered 28/7, 2017 at 13:5 Comment(0)
H
1

This happened to me when Java was updated to a 32-bit, but R required a 64-bit version.

The solution:

  1. Simply download the 64-bit version of Java
  2. install it.
  3. Restart RStudio
  4. Everything should work

Where to download 64-bit java:

enter image description here

Homegrown answered 20/12, 2021 at 4:5 Comment(0)
D
0

I guess your OS architecture and R architecture are not matching. If they are matching, the following might be the problem

It worked for me after i added the R library path to the environment variables list on my windows 7.

So,

Add 'C:/Users/Rohan-PC/Documents/R/win-library/3.1' this path to the Path variables list under System variables list(not to be confused with the PATH variable under User variables). Restart R and try loading the library. It works!!!!!

Durarte answered 19/6, 2017 at 11:9 Comment(0)
S
0

If you have both 64-bit R and 32-bit R versions installed, try from each one.

For me it worked on R 64bits but not R 32bits. But of course that depends on your java version (64-bit Java vs 32-bit Java).

Scalf answered 16/10, 2020 at 9:43 Comment(1)
Actually the problem seems to be related to 32 vs. 64 bit, but for an answer you should be more sure about the problem and the solution.Nitrification

© 2022 - 2024 — McMap. All rights reserved.