为什么 rJava 在使用 OpenJDK 7 的 Ubuntu 14.04 上不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23469061/
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-08-13 23:13:38  来源:igfitidea点击:

Why does rJava not work on Ubuntu 14.04 using OpenJDK 7?

javaropenjdkrjavaubuntu-14.04

提问by Dr. Mike

Hi I'm having issues with the rJava package from cran.

嗨,我遇到了来自 cran 的 rJava 包的问题。

I have installed

我已经安装

sudo apt-get install openjdk-7-jdk
sudo apt-get install r-cran-rjava

and ran

跑了

sudo R CMD javareconf
# Java interpreter : /usr/bin/java
# Java version     : 1.7.0_55
# Java home path   : /usr/lib/jvm/java-7-openjdk-amd64/jre
# Java compiler    : /usr/bin/javac
# Java headers gen.: /usr/bin/javah
# Java archive tool: /usr/bin/jar

I then try to run R and load rJava and get the following error:

然后我尝试运行 R 并加载 rJava 并收到以下错误:

R
> library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
Error: package or namespace load failed for ‘rJava'

I'm on Ubuntu 14.04 64 bit and am using R version 3.1.0 (2014-04-10) -- "Spring Dance"

我使用的是 Ubuntu 14.04 64 位,正在使用 R 版本 3.1.0 (2014-04-10) -- "Spring Dance"

UPDATE: Actually this is not specific to OpenJDK, I just tried oracle java 8 and got the same result. Also I found this workaround herewhich I am reluctant to use since it is indeed a workaround and doesn't really explain why it's necessary. The package system should have handled this in my opinion. Seems like libjvm.so is the problem and I have it located here

更新:实际上这不是 OpenJDK 特有的,我只是尝试了 oracle java 8 并得到了相同的结果。我也在这里找到了这个我不愿意使用的解决方法,因为它确实是一种解决方法,并没有真正解释为什么它是必要的。在我看来,包系统应该已经解决了这个问题。似乎 libjvm.so 是问题所在,我将其放在此处

/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/jamvm/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so
/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/libjvm.so

and for some reason rJava fails to find them despite updating with sudo R CMD javareconf.

并且由于某种原因,尽管使用 sudo R CMD javareconf 进行了更新,但 rJava 未能找到它们。

UPDATE 2: The plot thickens: If I run R as sudo it works.

更新 2:情节变厚:如果我将 R 作为 sudo 运行,它会起作用。

Thankful for pointers.

感谢指点。

采纳答案by persistentNoob

I had the same problem with a similar configuration (R 3.1.0, Ubuntu 12.10, 32-bit). I found the answer was in getting LD_LIBRARY_PATH set properly, as described here: error: unable to load installed packages just nowexcept that the subdirectory in question is 'client' not 'server'. So now I'm setting my environment like this:

我在使用类似的配置(R 3.1.0、Ubuntu 12.10、32 位)时遇到了同样的问题。我发现答案是正确设置 LD_LIBRARY_PATH,如下所述: 错误:刚才无法加载已安装的包,只是有问题的子目录是“客户端”而不是“服务器”。所以现在我设置我的环境是这样的:

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386:$JAVA_HOME/jre/lib/i386/client

回答by lapisdecor

I was able to solve this permanentelly using this answer: https://stackoverflow.com/a/25932828/3939832

我能够使用这个答案永久解决这个问题:https: //stackoverflow.com/a/25932828/3939832

This is usefull if you have Oracle java 7 or 8 installed. Exporting variables in my case was not a permanent solution. You should check what R is using as environment by doing:

如果您安装了 Oracle java 7 或 8,这将非常有用。在我的情况下导出变量不是一个永久的解决方案。您应该通过执行以下操作来检查 R 正在使用的环境:

Sys.getenv("JAVA_HOME")

and then you can use that environment by creating a java.conffile on /etc/ld.so.conf.d/as stated in the above answer link.

然后您可以通过创建上述答案链接中所述的java.conf文件来使用该环境on /etc/ld.so.conf.d/

回答by sagarambat

you can solve this problem by opening rstudio in super user like

您可以通过在超级用户中打开 rstudio 来解决这个问题

sudo rstudio

then inside R run

然后在 R 中运行

install.packages('rJava')

回答by sourav karwa

if you are using oracle java then use following command :

如果您使用的是 oracle java,请使用以下命令:

sudo R CMD javareconf

sudo R CMD javareconf

won't help use:

不会帮助使用:

sudo R CMD javareconf **JAVA_HOME**=(path where java home is located)

回答by Ajay Jadhav

I tried many things but didnt worked. Then I tried using

我尝试了很多东西,但没有奏效。然后我尝试使用

sudo rstudio

and then

进而

install.packages('rJava')

Its working. Coool

它的工作。酷

回答by Jyoti

Installing the rJava package on Ubuntu is not quite as simple as most other R packages. Some quick notes on how to do it(Source: https://www.r-bloggers.com/installing-rjava-on-ubuntu/).

在 Ubuntu 上安装 rJava 包并不像大多数其他 R 包那么简单。关于如何做的一些快速说明(来源:https: //www.r-bloggers.com/installing-rjava-on-ubuntu/)。

Install the Java Runtime Environment (JRE).

sudo apt-get install -y default-jre

Install the Java Development Kit (JDK).

sudo apt-get install -y default-jdk

Update where R expects to find various Java files.

sudo R CMD javareconf

Install the package.

> install.packages("rJava")

If you have a RStudio session open, then exit and restart it. This is important (a running RStudio session will not pick up these changes!).