1.前期准备:
2.编写代码:
编码内容:
install.packages("rJava") //安装rJava包(安装一次即可)
(1)library(rJava) //加载rJava包
(2)Sys.setenv(JAVA_HOME='JDK安装目录') //设置JDK环境
(3).jinit() //初始化java的JVM运行环境
(4).jaddClassPath("java类jar包绝对路径") //添加java类jar包绝对路径
(5)Java对象 <- .jnew("Java类的全名") //创建Java对象,java类的全名=包名+类名
(6)result <- .jcall(Java对象, returnSig="方法返回类型;", method="方法名") //调用java类方法
使用如下:此处使用时RStudio编译软件
> install.packages("rJava")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:https://cran.rstudio.com/bin/windows/Rtools/ trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.3/rJava_1.0-6.zip'
Content type 'application/zip' length 1299141 bytes (1.2 MB)
downloaded 1.2 MB程序包‘rJava’打开成功,MD5和检查也通过
下载的二进制程序包在
C:\Users\admin\AppData\Local\Temp\RtmpgFscIt\downloaded_packages里
载入需要的程辑包:rJava
> library(rJava)
> Sys.setenv(JAVA_HOME='D:/DevelopTool/jdk/jdk1.8')
> .jinit()
> .jaddClassPath("D:/RSpeech-call-javaCode/myclass/RCallJavaCodeTest.jar")
> RCallJavaCodeTest <- .jnew("com.hui.test.RCallJavaCodeTest")
> result <- .jcall(RCallJavaCodeTest, returnSig="Ljava/lang/String;", method="printMessage")
R���Ե���java������Է���......
>