“升级”到 OSX Yosemite 后 RStudio/R 中的 rJava 加载错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30738974/
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
rJava load error in RStudio/R after "upgrading" to OSX Yosemite
提问by eipi10
I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2. Immediately after the upgrade, when I opened R or RStudio I got a pop-up message saying that I needed to install Java 6. In addition, loading rJava
or any package that depends on rJava (e.g., xlsx
) caused RStudio to crash (R also crashed when I tried this by opening R.app
directly).
我最近从 OSX Mountain Lion “升级”到 Yosemite,从 R 3.1.3 升级到 3.2。升级后,当我打开 R 或 RStudio 时,我收到一个弹出消息,说我需要安装 Java 6。此外,加载rJava
或任何依赖于 rJava 的包(例如,xlsx
)导致 RStudio 崩溃(R 也崩溃了)当我通过R.app
直接打开尝试此操作时)。
After trying a few fixes found on Stack Overflow and elsewhere (more details below), I am at a point where loading rJava
or any package that depends on rJava
no longer causes R to crash, but results in the following error:
在尝试在 Stack Overflow 和其他地方找到的一些修复程序后(更多详细信息如下),我处于加载rJava
或依赖的任何包rJava
不再导致 R 崩溃的点,但导致以下错误:
library(rJava)
Error : .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.2/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava'
However, if I invoke R from the command line and then load rJava
or any package that depends on rJava
, it seems to work (or at least I don't get any error messages).
但是,如果我从命令行调用 R 然后加载rJava
或任何依赖于 的包rJava
,它似乎可以工作(或者至少我没有收到任何错误消息)。
I've tried a number of different attempted fixes, some of them a few times, and can't quite remember exactly what I did in what order (didn't realize this would be such a morass and wasn't really keeping track), but here's the gist of it:
我尝试了许多不同的尝试修复,其中一些尝试了几次,但不太记得我按什么顺序做了什么(没有意识到这会是一个烂摊子,并没有真正跟踪) ,但这是它的要点:
Added the following to my
.bash_profile
(per this SO answer):export JAVA_HOME="/usr/libexec/java_home -v 1.8"
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/serverReconfigured java from the command line as follows:
sudo R CMD javareconf -n
Checked
options("java.home")
and discovered this was set toNULL
. I tried setting it to the following (per this SO question):options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre")
Installed the latest Java Development Kit and reinstalled
rJava
from source (can't remember where I found that one).
将以下内容添加到我的
.bash_profile
(根据这个 SO 答案):导出 JAVA_HOME="/usr/libexec/java_home -v 1.8"
导出 LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server从命令行重新配置java如下:
须藤 R CMD javareconf -n
检查
options("java.home")
并发现此设置为NULL
。我尝试将其设置为以下内容(根据此 SO 问题):选项(“java.home”=“/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre”)
安装了最新的 Java Development Kit 并
rJava
从源代码重新安装(不记得我在哪里找到的)。
At some point while trying all of these, I was able to load rJava
without crashing R, but instead got the error message posted above. In addition, when I quit RStudio, it would seem to close normally, but then an "RStudio quit unexpectedly" message would pop up, indicating that the program had crashed while trying to close.
在尝试所有这些时,我能够在rJava
不崩溃 R 的情况下加载,但收到了上面发布的错误消息。另外,当我退出RStudio时,它似乎正常关闭,但随后会弹出“RStudio意外退出”消息,表明程序在尝试关闭时崩溃了。
I finally decided to install Java for OS X 2014-001(Java 6), as I seemed to be running out of options. Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared. However, I was still getting the .onLoad failed in loadNamespace() for 'rJava'
error message posted above.
我最终决定为 OS X 2014-001(Java 6)安装Java,因为我似乎没有什么选择了。现在,当我打开 R 或 RStudio 时,“此软件需要 Java 6”弹出消息不再出现。但是,我仍然收到.onLoad failed in loadNamespace() for 'rJava'
上面发布的错误消息。
In reviewing some of the posts I'd already looked at, I noticed another SO answerthat I'd missed before, which recommended opening RStudio with the following command line code that gives RStudio the correct path to java:
在查看我已经看过的一些帖子时,我注意到另一个我之前错过的SO 答案,它建议使用以下命令行代码打开 RStudio,为 RStudio 提供正确的 java 路径:
LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
That opened an RStudio window and I was also able to load rJava
and packages that depend on it without getting an error.
这打开了一个 RStudio 窗口,我也能够加载rJava
和依赖它的包而不会出错。
Lastly, I tried running R from the command line (which I hadn't done before). It turns out that on the command line, loading rJava
or any package that depends on rJava
works and does not throw any errors.
最后,我尝试从命令行运行 R(我以前没有这样做过)。事实证明,在命令行上,加载rJava
或任何依赖于的包都rJava
有效并且不会抛出任何错误。
So, I can now get rJava
to work if I open RStudio from the command line with the code that gives RStudio the java path (as noted above). However, I'd like to find a way to fix the underlying problem, whatever it may be, so that RStudio can be opened in the usual Mac way, without needing a command line kludge. I'm also concerned that having an old version of Java installed could cause problems down the road.
因此,rJava
如果我使用为 RStudio 提供 java 路径的代码从命令行打开 RStudio ,我现在可以开始工作(如上所述)。但是,我想找到一种方法来解决潜在的问题,无论它是什么,以便 RStudio 可以以通常的 Mac 方式打开,而无需命令行工具。我还担心安装旧版本的 Java 可能会导致问题。
Does anyone have any ideas about how to diagnose and solve this issue?
有没有人对如何诊断和解决这个问题有任何想法?
采纳答案by Alex Popov
I had the same problem and went through the same steps as you. The final step to allow starting RStudio through Finder/Spotlight was to link libjvm.dylib to /usr/local/lib:
我遇到了同样的问题,并经历了与您相同的步骤。允许通过 Finder/Spotlight 启动 RStudio 的最后一步是将 libjvm.dylib 链接到 /usr/local/lib:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
-f
flag is added to force overwriting existing file/link
-f
添加标志以强制覆盖现有文件/链接
回答by lalas
On OSX El Capitan 10.11, the user doesn't have permission to write to /usr/lib. So instead, use the following command:
在 OSX El Capitan 10.11 上,用户无权写入 /usr/lib。因此,请改用以下命令:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
回答by ignazw
Here you can download the legacy Java version 6 for El Capitan: https://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_USAfter doing so, the rJava package works for me.
您可以在这里下载 El Capitan 的旧版 Java 版本 6:https: //support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US 这样做后,rJava 包对我有用。
回答by yuan
You can load the libjvm.dylib explictly, by run like
您可以通过运行像显式加载 libjvm.dylib
dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
library(rJava)
回答by Sander W. van der Laan
Following command works:
以下命令有效:
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
After that, in RStudio, loading rJava works through loading of the "xlsx" package.
之后,在 RStudio 中,通过加载“xlsx”包来加载 rJava。
library("xlsx")
P.S. #1 Environment: Mac OS X El Capitan 10.11.3+ with RStudio 0.99.491+ and R 3.2.3+. (I've now tested this also on macOS Sierra (10.12) and R.3.3.1.)
PS #1 环境:Mac OS X El Capitan 10.11.3+ 与 RStudio 0.99.491+ 和 R 3.2.3+。(我现在也在 macOS Sierra (10.12) 和 R.3.3.1 上对此进行了测试。)
P.S. #2 I find that openxlsx
is much faster and doesn't rely on Java to work, so I'm now using that package. Hope that helps others.
PS #2 我发现它openxlsx
要快得多并且不依赖于 Java 来工作,所以我现在正在使用那个包。希望能帮助别人。
回答by Hamidreza Ahady Dolatsara
Here is the easy steps for it:
这是它的简单步骤:
- remove the rJava package: remove.packages(rJava)
- close R
- install latest Java on you mac
- open terminal and type this command: sudo R CMD javareconf
Open R and install rJava with this command:
install.packages("rJava", dependencies=TRUE, type="source")
- 删除 rJava 包:remove.packages(rJava)
- 关闭 R
- 在 Mac 上安装最新的 Java
- 打开终端并输入以下命令:sudo R CMD javareconf
打开 R 并使用以下命令安装 rJava:
install.packages("rJava", dependencies=TRUE, type="source")
回答by Nasser
Here what worked for me on MAC:
这是在 MAC 上对我有用的内容:
in your
~/.profile
or~/.bashrc
add this line:%export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
1.1
% source .profile
(or% source .bashrc
)% sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
remove.packages(rJava)
remove.packages(Rweka)
- From the terminal enter this command:
%sudo R CMD javareconf
install.packages("rJava", dependencies = TRUE, type = "source")
install.packages("rJava", dependencies = TRUE, type = "source")
- Load your
library("rJava", "RWeka")
在您的
~/.profile
或~/.bashrc
添加此行:%export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
1.1
% source .profile
(或% source .bashrc
)% sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
remove.packages(rJava)
remove.packages(Rweka)
- 从终端输入以下命令:
%sudo R CMD javareconf
install.packages("rJava", dependencies = TRUE, type = "source")
install.packages("rJava", dependencies = TRUE, type = "source")
- 加载你的
library("rJava", "RWeka")
Best of luck.
祝你好运。
回答by Gregory R. Warnes
This is how I configured it working properly on two Macs running Mac OS X El Capitan (10.11.6):
这是我在运行 Mac OS X El Capitan (10.11.6) 的两台 Mac 上正确配置它的方式:
Uninstall 'rJava' by issuing the following commands in a terminal window:
Rscript -e 'remove.packages("rJava")' sudo Rscript -e 'remove.packages("rJava")'
Download and install the Java software from Oracle: https://www.java.com/en/download/mac_download.jsp
Add the following lines to
/Users/<userid>/.bashrc
using your favorite editor:# Set JAVA_HOME so rJava package can find it export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
Close and restart any terminal, R, and RStudio windows (to pick up the changes to
.bashrc
).Run the following command in a Terminal window:
sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Run the following command in a Terminal window:
sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
通过在终端窗口中发出以下命令来卸载“rJava”:
Rscript -e 'remove.packages("rJava")' sudo Rscript -e 'remove.packages("rJava")'
从 Oracle 下载并安装 Java 软件:https: //www.java.com/en/download/mac_download.jsp
添加以下行以
/Users/<userid>/.bashrc
使用您喜欢的编辑器:# Set JAVA_HOME so rJava package can find it export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
关闭并重新启动任何终端、R 和 RStudio 窗口(以获取对 的更改
.bashrc
)。在终端窗口中运行以下命令:
sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
在终端窗口中运行以下命令:
sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
回答by mokjpn
Maybe another simple answer which does not touch your filesystem:
也许是另一个不涉及您的文件系统的简单答案:
$ install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
$ install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
(Substitute jdk1.8.0_131.jdk
with your JDK path.)
(替换jdk1.8.0_131.jdk
为您的 JDK 路径。)
回答by Jim
You should be able to use the CRAN rJava, without needing to recompile rJavaor do any additional steps by linking the shared library to the R frameworks library directory.
您应该能够使用 CRAN rJava,而无需重新编译rJava或通过将共享库链接到 R 框架库目录来执行任何其他步骤。
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Libraries