Java R 接口 (JRI) 设置

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

Java R Interface (JRI) Setup

javarinterfacejri

提问by user1415528

I am trying to setup the Java/R interface (bundled in the R "rJava" package), but I am having some trouble (despite trying many suggestions here and on other forums).

我正在尝试设置 Java/R 接口(捆绑在 R“rJava”包中),但我遇到了一些问题(尽管在这里和其他论坛上尝试了很多建议)。

I am running Windows 7, jdk1.7.0_05, Eclipse, R 2.15.2. These are all 64 bit installations.

我正在运行 Windows 7、jdk1.7.0_05、Eclipse、R 2.15.2。这些都是 64 位安装。

The steps I have followed to attempt to get the example running are as follows:

我为尝试运行示例而遵循的步骤如下:

  1. Install rJava within the R GUI: install.packages('rJava') - installs to C:\Users\USERNAME\Documents\R\win-library\2.15\rJava\
  2. Setup environment variables: R_HOME = "C:\Program Files\R\R-2.15.2", additions to PATH = "C:\Program Files\Java\jdk1.7.0_05\bin";"C:\Program Files\Java\jdk1.7.0_05\jre\bin\server";"C:\Program Files\R\R-2.15.2\bin\x64";"C:\Users\USERNAME\Documents\R\win-library\2.15\rJava\jri\x64"
  3. Setup example project in Eclipse, setting VM argument: -Djava.library.path="C:\Users\USERNAME\Documents\R\win-library\2.15\rJava\jri\x64"
  4. Restart R and load rJava with command: libraries('rJava')
  5. Run the example program, obtaining the following exception:
  1. 在 R GUI 中安装 rJava: install.packages('rJava') - 安装到 C:\Users\USERNAME\Documents\R\win-library\2.15\rJava\
  2. 设置环境变量:R_HOME = "C:\Program Files\R\R-2.15.2", 添加到 PATH = "C:\Program Files\Java\jdk1.7.0_05\bin";"C:\Program Files\ Java\jdk1.7.0_05\jre\bin\server";"C:\Program Files\R\R-2.15.2\bin\x64";"C:\Users\USERNAME\Documents\R\win-library\ 2.15\rJava\jri\x64"
  3. 在 Eclipse 中设置示例项目,设置 VM 参数:-Djava.library.path="C:\Users\USERNAME\Documents\R\win-library\2.15\rJava\jri\x64"
  4. 重新启动 R 并使用命令加载 rJava:libraries('rJava')
  5. 运行示例程序,得到如下异常:

Cannot find JRI native library! Please make sure that the JRI native library is in a directory listed in java.library.path.

找不到 JRI 本地库!请确保 JRI 本机库位于 java.library.path 中列出的目录中。

java.lang.UnsatisfiedLinkError: C:\Users\USERNAME\Documents\R\win-library.15\rJava\jri\x64\jri.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(Unknown Source)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
    at test.JRITest.main(JRITest.java:64)

I have verified "java.library.path" is set as expected since: System.out.println("JLP = " + System.getProperty("java.library.path")); outputs the correct path.

我已经验证“java.library.path”是否按预期设置,因为: System.out.println("JLP = " + System.getProperty("java.library.path")); 输出正确的路径。

I think rJava may not have installed fully (have tried reinstalling), but I'm not sure how to check this. The only JAR in "...\rJava\jri" or any of its subdirectories is "JRI.jar" and the examples directory seems incomplete.

我认为 rJava 可能没有完全安装(已尝试重新安装),但我不确定如何检查。“...\rJava\jri”或其任何子目录中唯一的 JAR 是“JRI.jar”,示例目录似乎不完整。

Does anyone have suggestions on what may be going wrong here?

有没有人对这里可能出现的问题提出建议?

It seems the JRI website (http://www.rforge.net/JRI/index.html) has been down today, not sure if anyone knows another source to download from (other than direct from CRAN).

JRI 网站(http://www.rforge.net/JRI/index.html)今天似乎已经关闭,不确定是否有人知道其他下载来源(直接从 CRAN 下载除外)。

Thanks.

谢谢。

回答by Oguz

Try to copy all dll files(*.dll) which in R directory to JAVA_PATH/bin. After that load the the jri native lib like:

尝试将 R 目录中的所有 dll 文件(*.dll)复制到 JAVA_PATH/bin。之后加载 jri 本机库,如:

//Right under your R class
   static {
    System.loadLibrary("jri");      
   }

I had the same problem and solved it by this method.

我遇到了同样的问题并通过这种方法解决了它。

Thx

谢谢

回答by Leo-from-Switzerland

Here's another option: add the directories of rJava and R where the dlls reside into the PATH variable, either at the Computer level or in an ant script like this for example (x64 for 64 bit installs, i386 for 32 bit, depending on your local installation):

这是另一个选项:将 dll 所在的 rJava 和 R 的目录添加到 PATH 变量中,无论是在计算机级别还是在像这样的 ant 脚本中(例如,x64 用于 64 位安装,i386 用于 32 位,取决于您的本地安装):

<java fork="true" classname="${run.class}">
  <env key="PATH" path="C:/Users/myuser/Documents/R/win-library/3.0/rJava/jri/x64;C:/Program Files/R/R-3.0.2/bin/x64"/>
  <classpath>
     <path refid="run.test.classpath"/>
  </classpath>
</java>

回答by greenish

Fellows, after few days of trying and failing I figured out my ultimate solution like:

伙计们,经过几天的尝试和失败,我想出了我的最终解决方案,例如:

  • the following files must be copied to directory C:/Program Files/RapidMiner/RapidMiner Studio/jre/bin/:

    • jri.dllfrom C:/Users/Public/Documents/Documentos/R/win-library/3.0/rJava/jri/x64/
    • all files from C:/Program Files/R/R-3.0.3/bin/x64/
  • so on RM Studio Toolstab Preferencesoption, Start-uptab, after checking Initialize extensionsthe Extensions directorymust be C:\Program Files\RapidMiner\RapidMiner Studio\jre\bin\R.exeand on Rtab Native libmust be C:\Program Files\RapidMiner\RapidMiner Studio\jre\bin\jri.dll

  • 必须将以下文件复制到目录C:/Program Files/RapidMiner/RapidMiner Studio/jre/bin/

    • jri.dllC:/Users/Public/Documents/Documentos/R/win-library/3.0/rJava/jri/x64/
    • 所有文件来自 C:/Program Files/R/R-3.0.3/bin/x64/
  • 等RM工作室Tools标签Preferences选项,Start-up选项卡,之后检查Initialize extensionsExtensions directory必须C:\Program Files\RapidMiner\RapidMiner Studio\jre\bin\R.exeR标签Native lib必须C:\Program Files\RapidMiner\RapidMiner Studio\jre\bin\jri.dll

Easy, isn't it? Thank you all for the advices.

很简单,不是吗?谢谢大家的建议。

回答by Hans

I got same issue. Resolved by add R bin to path(my OS is x64).

我有同样的问题。通过将 R bin 添加到路径来解决(我的操作系统是 x64)。

path=C:\Program Files\R\R-3.2.2\bin\x64

Anyway, you can find out which dll is the dependent one by use this tool http://dependencywalker.com/

无论如何,您可以通过使用此工具http://dependencywalker.com/找出哪个 dll 是依赖的

回答by Everton Reis

After reading a lot of posts and trying a lot of different solutions, it is finally working. I'm using Windows 7 64bits and I'll list the steps that I did to make it work properly.

在阅读了很多帖子并尝试了很多不同的解决方案后,它终于奏效了。我正在使用 Windows 7 64 位,我将列出我为使其正常工作所做的步骤。

  1. Install package rJava using R install.packages("rJava")
  2. Right-click on My Computer > Properties > Advanced System Settings > Environment Variables...
  3. Find Var PATH or CLASSPATH and Edit, add the following paths: C:\Program Files\R\R-3.4.0\bin\x64;C:\Users\ [User Name]\Documents\R\win-library\3.4\rJava\jri\x64
  4. Go to C:\Program Files\R\R-3.4.0\bin\x64 and copy all .dll files (here they are: R.dll,Rblas.dll,Rgraphapp.dll,Riconv.dll,Rlapack.dll)
  5. Past them at C:\Program Files\Java\jdk1.8.0_144\bin
  6. When you create your java project, remeber to add the JRI.jar, JRIEngine.jar and Rengine.jar to your project build path. They are located at C:\Users\ [User name]\Documents\R\win-library\3.4\rJava\jri
  7. After doing all this, I was still getting an error, that was solved when I went to C:\Users\ [User Name]\Documents\R\win-library\3.4\rJava\jri\x64 and copy the jri.dll and past inside my Java Project, like the following image:
  1. 使用 R 安装包 rJava install.packages("rJava")
  2. 右键单击我的电脑 > 属性 > 高级系统设置 > 环境变量...
  3. 找到 Var PATH 或 CLASSPATH 并编辑,添加以下路径:C:\Program Files\R\R-3.4.0\bin\x64;C:\Users\ [用户名]\Documents\R\win-library\3.4 \rJava\jri\x64
  4. 转到 C:\Program Files\R\R-3.4.0\bin\x64 并复制所有 .dll 文件(这里是:R.dll、Rblas.dll、Rgraphapp.dll、Riconv.dll、Rlapack.dll)
  5. 通过它们在 C:\Program Files\Java\jdk1.8.0_144\bin
  6. 创建 java 项目时,请记住将 JRI.jar、JRIEngine.jar 和 Rengine.jar 添加到项目构建路径中。它们位于 C:\Users\ [用户名]\Documents\R\win-library\3.4\rJava\jri
  7. 做完这一切后,我仍然得到一个错误,当我去 C:\Users\ [用户名]\Documents\R\win-library\3.4\rJava\jri\x64 并复制 jri.dll 时解决了并在我的 Java Project 中过去,如下图所示:

enter image description here

在此处输入图片说明

Here is a code example and its output:

这是一个代码示例及其输出:

enter image description here

在此处输入图片说明

This example was retrieved from: http://codophile.com/2015/04/15/how-to-integrate-r-with-java-using-rjava/

此示例检索自:http: //codophile.com/2015/04/15/how-to-integrate-r-with-java-using-rjava/