简体   繁体   中英

.onLoad failed in loadNamespace() for 'rJava' when installing a package

I have a package "javaOnLoadFailed" (just a minimal package for testing my issue, hence the weird name) which imports rJava. I get 'rJava' errors when I try to either check() or install() the package, even though require(rJava) itself works fine.

install() gives the following errors:

> install()
Installing javaOnloadFailed
"C:/Program Files/R/R-3.2.0/bin/x64/R" --no-site-file --no-environ --no-save  \
--no-restore CMD INSTALL  \
"C:/Projects/stackoverflow/javaOnloadFailed/javaOnLoadFailed"  \
--library="C:/Users/adb2018/Documents/R/win-library/3.2" --with-keep.source  \
--install-tests 

* installing *source* package 'javaOnloadFailed' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
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/adb2018/Documents/R/win-library/3.2/rJava/libs/i386/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Error: loading failed
Execution halted
*** arch - x64
ERROR: loading failed for 'i386'
* removing 'C:/Users/adb2018/Documents/R/win-library/3.2/javaOnloadFailed'
Error: Command failed (1)

I am using R 3.2.0 from within Architect, with sessionInfo():

R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_1.7.0.9000 rj_2.0.3-2         

loaded via a namespace (and not attached):
[1] tools_3.2.0   rj.gd_2.0.0-1

The Java environment variable is empty

> Sys.getenv('JAVA')
[1] ""

Based on a suggestion , I tried setting the JAVA environment variable to point to the 64 Bit version of Java (because I am running R 64 bit, as you could see from the sessionInfo, but that doesn't work:

> Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_45')
> install()
Installing javaOnloadFailed
"C:/Program Files/R/R-3.2.0/bin/x64/R" --no-site-file --no-environ --no-save  \
  --no-restore CMD INSTALL  \
  "C:/Projects/stackoverflow/javaOnloadFailed/javaOnLoadFailed"  \
  --library="C:/Users/adb2018/Documents/R/win-library/3.2" --with-keep.source  \
  --install-tests 

* installing *source* package 'javaOnloadFailed' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
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/adb2018/Documents/R/win-library/3.2/rJava/libs/i386/rJava.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Error: loading failed
Execution halted
*** arch - x64
ERROR: loading failed for 'i386'
* removing 'C:/Users/adb2018/Documents/R/win-library/3.2/javaOnloadFailed'
Error: Command failed (1)

I then tried to set the JAVA environment variable such that it points to the 32 bit version of Java on my system, and then it works!

> Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre1.8.0_45\\')
> install()
Installing javaOnloadFailed
"C:/Program Files/R/R-3.2.0/bin/x64/R" --no-site-file --no-environ --no-save  \
  --no-restore CMD INSTALL  \
  "C:/Projects/stackoverflow/javaOnloadFailed/javaOnLoadFailed"  \
  --library="C:/Users/adb2018/Documents/R/win-library/3.2" --with-keep.source  \
  --install-tests 

* installing *source* package 'javaOnloadFailed' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (javaOnloadFailed)

[INFO] Updating the R environment index started...

[INFO] The R environment index was updated successfully.

I don't quite understand why I need to point to Java 32 bit to make R 64 bit work, but that's what seems to be the case.

By the way, don't stumble over my package name "javaOnLoadFailed". I just created a minimal package with that name to test the problem.

Many packages fail install because they are not meant to run on i386 platform but the standard installation process tries to do that. Users waste a lot of time with jvm.dll and PATH and JAVA_HOME when the real fix is to force the installed to just forget about i386. Use option for install.packages. (this also works when drat library is used. (credit goes to Dason)

install.packages("SqlRender",INSTALL_opts="--no-multiarch")

Remember the problem is that R doesn't know where jvm.dll is. When you set JAVA_HOME use the path of the directory that contains the dll file. You can find it in bin\\client or bin\\server . The command then becomes:

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

I tried remove the JAVA_HOME system variable and it works. So that when you launch R, Sys.getenv("JAVA_HOME") gives you "" .

I have found the same problem when trying to install a package only available in Github.
This solved for me:

devtools::install_github("snowflakedb/dplyr-snowflakedb", INSTALL_opts=c("--no-multiarch"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM