java R,加载 rJava 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44157869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:59:45  来源:igfitidea点击:

R, loading rJava error

javarrjava

提问by MrMax

I'm running R (version 3.4.0) on macOS sierra (10.12.5) and I'm trying to use the xlsx library which requires the use of the rJava library.

我在 macOS sierra (10.12.5) 上运行 R(版本 3.4.0),我正在尝试使用需要使用 rJava 库的 xlsx 库。

When I try to load rJava using library(rJava)in the R console (no difference if in RStudio or in the terminal), I get the following error message which I'm having trouble making sense of:

当我尝试library(rJava)在 R 控制台中加载 rJava 时(在 RStudio 或终端中没有区别),我收到以下错误消息,我无法理解:

Fehler: package or namespace load failed for ‘rJava':
 .onLoad in loadNamespace() für 'rJava' fehlgeschlagen, Details:
  Aufruf: dyn.load(file, DLLpath = DLLpath, ...)
  Fehler: kann shared object '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so' nicht laden:
  dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
  Reason: image not found

Googling the problem I found a number of posts on somewhat similar (but each time slightly different problems), this SO questionbeing the most promising among them.

谷歌搜索这个问题我发现了一些关于有点相似(但每次都略有不同的问题)的帖子,这个问题是其中最有希望的。

Since the steps of the proposed require adaptation to my case and I'm kind of a novice, I wasn't able to implement them. What can I do?

由于建议的步骤需要适应我的情况,而且我是新手,因此我无法实施它们。我能做什么?

Additional information:

附加信息:

The shell command java -versionreturns:

shell 命令java -version返回:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

And the shell command R CMD javareconfreturns:

shell 命令R CMD javareconf返回:

Java interpreter : /usr/bin/java
Java version     : 1.8.0_131
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on macOS

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/jdk1.8.0_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../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/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation


JAVA_HOME      /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
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.

Any help will be greatly appreciated!

任何帮助将不胜感激!

回答by MrMax

It works!

有用!

After installing and reinstalling different java versions and reinstalling rJava with the R command install.packages("rJava")I got to the point where loading the library in the R console (shell window) didn't throw an error message anymore.

在安装并重新安装不同的 Java 版本并使用 R 命令重新安装 rJava 后,install.packages("rJava")我到了在 R 控制台(shell 窗口)中加载库不再抛出错误消息的地步。

After that I was able to use the answer in this postto get it to work in RStudio by running this shell command:

之后,我可以使用这篇文章中的答案通过运行以下 shell 命令使其在 RStudio 中工作:

sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

Not too complicated after all! Thank you for everyones patience.

毕竟不是太复杂!感谢大家的耐心等待。

回答by vinay karagod

JAVA_HOME path is different for different version of MAC OS. In some cases there is /jrefolder and in some cases it is not after /Homedirector manually go towards /serverfolder with CD command in terminal and using pwdcopy the entire path use that to set the java.home then you can easily load rJavalibrary. This is how I was able to fix the issue

不同版本的MAC OS JAVA_HOME 路径不同。在某些情况下有/jre文件夹,在某些情况下它不是在/Home导演手动/server使用终端中的 CD 命令进入文件夹并使用pwd复制整个路径使用它来设置 java.home 然后您可以轻松加载rJava库。这就是我解决问题的方法

options("java.home"="/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib")

Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')

dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib/server/libjvm.dylib')

library(rJava)