无法在MacOS Mojave和R 3.5.2上加载rJava [英] Can't load rJava on MacOS Mojave and R 3.5.2
问题描述
我已经问过此处中阅读了答案和此处,但无法实现其中的任何一个(部分原因是实际上没有答案可追溯所有步骤.
I have asked a similar question before, but didn't get any answers that I could actually implement. I've also read the answers here and here, but couldn't implement any of them (partially because no answer actually retraces all of its steps.
尝试在R 3.5.2和MacOS Mojave 10.14.3上加载rJava时出现错误:
I'm getting an error when trying to load rJava on R 3.5.2 and MacOS Mojave 10.14.3:
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
尝试的解决方案
我首先尝试了此解决方案,但该方法无效.然后,根据此答案,我尝试了以下操作:
Attempted Solutions
I first tried this solution, which didn't work. Then, based on this answer, I tried the following:
在终端上输入以下内容:
Entered the following on Terminal:
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
然后在R上尝试以下操作:
Then tried the following on R:
> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home")
>
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
>
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
>
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
我也尝试过:
> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/bin")
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
过去,我已经成功降级到R 3.1,但是我不想每次我都想使用rJava时都这样做.
In the past, I've downgraded succesfully to R 3.1, but I'd rather not have to do that every time I want to use rJava
推荐答案
这是对我有用的设置
-
macOS Mojave-10.14.3
macOS Mojave - 10.14.3
XQuartz-版本2.7.11- https://www.xquartz.org
XQuartz - version 2.7.11 - https://www.xquartz.org
-
R-版本5.3.2
R - version 5.3.2
> R --version
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.
Java-11.0.1
Java - 11.0.1
> java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
rJava-从CRAN安装
rJava - installed from CRAN
> > install.packages("rJava")
--- Please select a CRAN mirror for use in this session ---
Fontconfig warning: ignoring UTF-8: not a valid region tag
trying URL 'https://cloud.r-project.org/bin/macosx/el-
capitan/contrib/3.5/rJava_0.9-10.tgz'
Content type 'application/x-gzip' length 739259 bytes (721 KB)
==================================================
downloaded 721 KB
The downloaded binary packages are in
/var/folders/...
> library(rJava)
>
更新
在您的情况下,似乎缺少在rJava的安装/编译过程中使用的JDK.您可以从消息中得知:
In your case, it looks like JDK used during installation/compilation of rJava is missing. You can tell that from the message:
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
确保此文件存在:
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
可能丢失了.您在这里有两个选择:安装Java 11.0.1,重新配置R.
Probably, it's missing. You have two options here: install Java 11.0.1, reconfigure R.
您可以使用/usr/libexec/java_home -V
再现最初的问题,并加以解决
-
让我们假装我们已删除JDK 11.0.1
Let's pretend we have removed JDK 11.0.1
> pwd /Library/Java/JavaVirtualMachines
> tree -L 1
.
|-- jdk-11.0.1.jdk~
`-- jdk-11.0.2.jdk
> java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
让我们尝试加载rJava
Let's try to load rJava
> R
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
...
...
...
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
让我们修复
Let's fix it
> sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 11.0.2
Java home path : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include/darwin -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
现在应该可以正常工作
It should work now
> R
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
...
...
...
> library(rJava)
> quit()
这篇关于无法在MacOS Mojave和R 3.5.2上加载rJava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!