One, the software environment
win10 64位 + eclipse + jdk 1.7
Second, the steps of using the JRI method
2.1 First install rJava (two ways)
(1) Install using the command install.packages("rJava");
(2) Install using the graphical interface of RGui;
Package --> Install Package --> Select CRAN (China (beijing 4) [https]) --> Select rJava to confirm;
2.2 Modify environment variables;
(1) Add the R_HOME variable to the path (D:\Program Files\R\R-3.2.3)
(2) Addition in the current path% R_HOME% \ bin;% R_HOME% \ bin \ i386;% R_HOME% \ library \ rJava \ jri;% R_HOME% \ library \ rJava \ jri \ i386
2.3 Create a java project (Rtest)
(1), first go to www.rforge.net/rJava to download the three files JRI.jar, REngine.jar and JRIEngine.jar, and import them into Rtest;
(2), copy the existing examples (rtest.java and rtest2.java) in jri/examples in the rJava package installation directory to Rtest;
(3), run the test;
Third, the operation result failed!
3.1 The following exception occurs
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 ;测试成功!