简体   繁体   English

“升级”到 OSX Yosemite 后 RStudio/R 中的 rJava 加载错误

[英]rJava load error in RStudio/R after "upgrading" to OSX Yosemite

I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2.我最近从 OSX Mountain Lion “升级”到 Yosemite,从 R 3.1.3 升级到 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 (eg, xlsx ) caused RStudio to crash (R also crashed when I tried this by opening R.app directly).升级后,当我打开 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 ):在我的.bash_profile添加了以下内容(根据这个 SO 答案):

    export JAVA_HOME="/usr/libexec/java_home -v 1.8"导出 JAVA_HOME="/usr/libexec/java_home -v 1.8"
    export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server导出 LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server

  • Reconfigured java from the command line as follows:从命令行重新配置java如下:

    sudo R CMD javareconf -n须藤 R CMD javareconf -n

  • Checked options("java.home") and discovered this was set to NULL .检查options("java.home")并发现它被设置为NULL I tried setting it to the following (per this SO question ):我尝试将其设置为以下内容(根据此 SO 问题):

    options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre")选项(“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).安装了最新的 Java 开发工具包并从源代码重新安装了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.在尝试所有这些时,我能够在不使 R 崩溃的情况下加载rJava ,但收到了上面发布的错误消息。 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.另外,当我退出RStudio时,它似乎正常关闭,但随后会弹出“RStudio意外退出”消息,表明程序在尝试关闭时崩溃了。

I finally decided to install Java for OS X 2014-001 (Java 6), as I seemed to be running out of options.我最终决定为 OS X 2014-001 (Java 6) 安装Java ,因为我似乎没有什么选择了。 Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared.现在,当我打开 R 或 RStudio 时,“此软件需要 Java 6”弹出消息不再出现。 However, I was still getting the .onLoad failed in loadNamespace() for 'rJava' error message posted above.但是, .onLoad failed in loadNamespace() for 'rJava'上面发布的.onLoad failed in loadNamespace() for 'rJava'错误消息,我仍然.onLoad failed in loadNamespace() for 'rJava'收到.onLoad failed in loadNamespace() for 'rJava'

In reviewing some of the posts I'd already looked at, I noticed another SO answer that 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).最后,我尝试从命令行运行 R(我以前没有这样做过)。 It turns out that on the command line, loading rJava or any package that depends on rJava works and does not throw any errors.事实证明,在命令行上,加载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).因此,如果我从命令行使用为 RStudio 提供 java 路径的代码打开 RStudio,我现在可以让rJava工作(如上所述)。 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.但是,我想找到一种方法来解决潜在的问题,无论它是什么,以便 RStudio 可以以通常的 Mac 方式打开,而无需命令行工具。 I'm also concerned that having an old version of Java installed could cause problems down the road.我还担心安装旧版本的 Java 可能会导致问题。

Does anyone have any ideas about how to diagnose and solve this issue?有没有人对如何诊断和解决这个问题有任何想法?

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标志以强制覆盖现有文件/链接

On OSX El Capitan 10.11, the user doesn't have permission to write to /usr/lib.在 OSX El Capitan 10.11 上,用户无权写入 /usr/lib。 So instead, use the following command:因此,请改用以下命令:

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

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)

On macOS High Sierra (10.13.1) and Java Version 9 you have to use a slightly different JVM path (notice the missing jre folder in the path compared to the instructions for earlier Java versions):在 macOS High Sierra (10.13.1) 和 Java 版本 9 上,您必须使用略有不同的 JVM 路径(注意与早期 Java 版本的说明相比,路径中缺少的jre文件夹):

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

You also have to notify R about the JVM:您还必须通知 R 有关 JVM 的信息:

MY_R_VERSION=$(Rscript -e "cat(with(R.version, sprintf('%s.%s', major, substring(minor, 1, 1))))")
ln -s /usr/local/lib/libjvm.dylib /Library/Frameworks/R.framework/Versions/$MY_R_VERSION/Resources/lib/

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

PS #1 Environment: Mac OS X El Capitan 10.11.3+ with RStudio 0.99.491+ and R 3.2.3+. PS #1 环境:Mac OS X El Capitan 10.11.3+ 与 RStudio 0.99.491+ 和 R 3.2.3+。 (I've now tested this also on macOS Sierra (10.12) and R.3.3.1.) (我现在也在 macOS Sierra (10.12) 和 R.3.3.1 上对此进行了测试。)

PS #2 I find that openxlsx is much faster and doesn't rely on Java to work, so I'm now using that package. PS #2 我发现openxlsx快得多并且不依赖于 Java 来工作,所以我现在正在使用那个包。 Hope that helps others.希望能帮助别人。

您应该能够使用 CRAN rJava,而无需重新编译rJava或通过将共享库链接到 R 框架库目录来执行任何其他步骤。

sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Libraries

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 上正确配置它的方式:

  1. Uninstall 'rJava' by issuing the following commands in a terminal window:通过在终端窗口中发出以下命令来卸载“rJava”:

     Rscript -e 'remove.packages("rJava")' sudo Rscript -e 'remove.packages("rJava")'
  2. Download and install the Java software from Oracle: https://www.java.com/en/download/mac_download.jsp从 Oracle 下载并安装 Java 软件: https : //www.java.com/en/download/mac_download.jsp

  3. Add the following lines to /Users/<userid>/.bashrc using your favorite editor:使用您喜欢的编辑器将以下几行添加到/Users/<userid>/.bashrc

     # Set JAVA_HOME so rJava package can find it export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
  4. Close and restart any terminal, R, and RStudio windows (to pick up the changes to .bashrc ).关闭并重新启动任何终端、R 和 RStudio 窗口(以获取对.bashrc的更改)。

  5. Run the following command in a Terminal window:在终端窗口中运行以下命令:

     sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
  6. Run the following command in a Terminal window:在终端窗口中运行以下命令:

     sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'

Here is the easy steps for it:这是它的简单步骤:

  1. remove the rJava package: remove.packages(rJava)删除 rJava 包:remove.packages(rJava)
  2. close R关闭 R
  3. install latest Java on you mac在 Mac 上安装最新的 Java
  4. open terminal and type this command: sudo R CMD javareconf打开终端并输入以下命令:sudo R CMD javareconf
  5. Open R and install rJava with this command:打开 R 并使用以下命令安装 rJava:

    install.packages("rJava", dependencies=TRUE, type="source") install.packages("rJava", dependencies=TRUE, type="source")

The image not found issue exists with a brand-new install on OSX High Sierra with the latest Java SE 10 JDK.使用最新的 Java SE 10 JDK 在 OSX High Sierra 上进行全新安装时存在未找到图像的问题。

I was able to solve the path issue with rJava using the fix found on the rJava Github issues page: https://github.com/su/rJava/issues/78我能够使用在 rJava Github 问题页面上找到的修复程序解决 rJava 的路径问题: https : //github.com/su/rJava/issues/78

R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers

Here what worked for me on MAC:这是在 MAC 上对我有用的内容:

  1. in your ~/.profile or ~/.bashrc add this line: %export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home在你的~/.profile~/.bashrc添加这一行: %export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home

    1.1 % source .profile (or % source .bashrc ) 1.1 % source .profile (或% source .bashrc

  2. % sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
  3. remove.packages(rJava)
  4. remove.packages(Rweka)
  5. From the terminal enter this command: %sudo R CMD javareconf从终端输入以下命令: %sudo R CMD javareconf
  6. install.packages("rJava", dependencies = TRUE, type = "source")
  7. install.packages("rJava", dependencies = TRUE, type = "source")
  8. Load your library("rJava", "RWeka")加载您的library("rJava", "RWeka")

Best of luck.祝你好运。

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

(Substitute jdk1.8.0_131.jdk with your JDK path.) (用您的 JDK 路径替换jdk1.8.0_131.jdk 。)

Check the location of libjvm.dylib file.检查 libjvm.dylib 文件的位置。

Try this, in my case this worked:试试这个,在我的情况下这有效:

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

I went through all the troubleshooting I could find, then installed jdk-11.0.1_osx-x64_bin.dmg from: Oracle downloads我完成了我能找到的所有故障排除,然后从以下位置安装了 jdk-11.0.1_osx-x64_bin.dmg: Oracle 下载

Everything worked perfectly after that.之后一切都运行得很完美。

Check the version of the library that was not loaded when you run library('rJava') and match it to the java version you need installed.检查运行 library('rJava') 时未加载的库版本,并将其与您需要安装的 Java 版本匹配。

I noticed that 'rJava' is pretty strict about JDK version while loading in Rstudio.我注意到在 Rstudio 中加载时,“rJava”对 JDK 版本非常严格。
In my case (Mac OS. 10.14.6 - Mojave) I had installed jdk-13 which was tested against jdk-11.在我的情况下(Mac OS。10.14.6 - Mojave)我安装了 jdk-13,它针对 jdk-11 进行了测试。
I have linked my JDK version to a non-existent directory that was assumed to be valid by Rstudio (JDK 11):我已将我的 JDK 版本链接到一个不存在的目录,该目录被 Rstudio(JDK 11)认为是有效的:

sudo ln -sf /Library/Java/JavaVirtualMachines/jdk-13.jdk/ /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk

and suprisingly it works like a charm...令人惊讶的是它就像一个魅力......
BTW., there was no problem to run 'rJava' following the previous instructions in native R.顺便说一句,按照本机 R 中的先前说明运行“rJava”没有问题。

您可以在这里下载 El Capitan 的旧版 Java 版本 6: https : //support.apple.com/kb/DL1572? viewlocale=en_US&locale=en_US 这样做后,rJava 包对我有用。

This line below has solved the same problem I was having with rJava package as some others in this discussion did.下面的这一行解决了我在使用rJava包时遇到的与本讨论中的其他一些问题相同的问题。 I'm sure there are more than one solution to this problem and I sincerely thank them all for their contributions because sometimes one line like the one below saves a lot of time from going to trash!我相信这个问题有不止一种解决方案,我真诚地感谢他们所有人的贡献,因为有时像下面这样的一行可以节省很多时间,以免浪费时间!

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

Run the following on the command line: sudo R CMD javareconf在命令行上运行以下命令: sudo R CMD javareconf

Several solutions above have mentioned this, but they also suggest that one remove and subsequently re-install the rJava package.上面的几个解决方案都提到了这一点,但他们也建议删除并随后重新安装rJava包。 I found those additional steps to be unnecessary.我发现这些额外的步骤是不必要的。

您可以使用以下命令更改 R 中的引用:

sudo install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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