.onLoad failed in loadNamespace() for 'rJava&#

2019-02-11 18:32发布

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

标签: r rjava
5条回答
放我归山
2楼-- · 2019-02-11 19:14

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.

查看更多
男人必须洒脱
3楼-- · 2019-02-11 19:15

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

查看更多
Melony?
4楼-- · 2019-02-11 19:16

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"))
查看更多
我命由我不由天
5楼-- · 2019-02-11 19:18

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')

查看更多
萌系小妹纸
6楼-- · 2019-02-11 19:20

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")
查看更多
登录 后发表回答