一,软件环境
win10 64位 + eclipse + jdk 1.7
二,使用JRI方式的步骤
2.1 首先安装rJava(两种方式)
(1) 使用命令install.packages("rJava")安装 ;
(2) 使用RGui的图形化界面安装;
程序包 --> 安装程序包 --> 选择CRAN (China (beijing 4) [https]) --> 选择rJava 确定即可;
2.2 修改环境变量;
(1) 在path中添加R_HOME变量(D:\Program Files\R\R-3.2.3)
(2) 在path中添加 %R_HOME%\bin;%R_HOME%\bin\i386;%R_HOME%\library\rJava\jri;%R_HOME%\library\rJava\jri\i386
2.3 建立java工程(Rtest)
(1), 首先到www.rforge.net/rJava下载JRI.jar、REngine.jar和JRIEngine.jar这三个文件,导入到Rtest中;
(2), 拷贝 rJava包安装目录下的jri/examples里有现成的例子(rtest.java和rtest2.java)到Rtest中;
(3), 运行测试;
三 ,运行结果失败!
3.1 出现如下异常
Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.
java.lang.UnsatisfiedLinkError: D:\Program Files\R\R-3.2.3\library\rJava\jri\jri.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
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 com.rtest.rtest.main(rtest.java:62)
大致意思就是 64位的JVM不能加载32位的 .dll文件;
然后我把原来的jri.dll文件替换成64位的jri.dll文件,测试运行控制台抛出如下异常:
Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.
java.lang.UnsatisfiedLinkError: D:\Program Files\R\R-3.2.3\library\rJava\jri\jri.dll: %1 不是有效的 Win32 应用程序。
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1807)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1732)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at org.rosuda.JRI.Rengine.(Rengine.java:19)
at R_Test.rtest.main(rtest.java:63)
后来观察R和rJava的安装目录发现,目录下包含x64和i386(32位)文件夹,所以把2.2步骤中的环境变量全部修改指向x64文件夹;具体如下:
%R_HOME%\bin;%R_HOME%\bin\x64;%R_HOME%\library\rJava\jri;%R_HOME%\library\rJava\jri\x64
然后重新启动eclipse(重新加载java.library.path变量) 很重要!!!
运行rtest.java和rtest2.java ;测试成功!