rjava .jcall 问题
我目前正在开发一个 R 包以将 java 代码集成到 R 中。但是,我在尝试正确调用 java 类方法时遇到问题。到目前为止我已经独立开发了一个java程序并编译成class文件,然后打包为jar文件。我的代码示例如下:
library(rJava)
.jinit()
.onLoad <- function(lib, pkg) {
pathToSdk <- paste(system.file(package = "mailViz") , "C:\\path\\to\\libs", sep="")
jarPaths <- c(paste(pathToSdk, "mail.jar", sep=""),
paste(pathToSdk, "mailReader.jar", sep="")
)
.jpackage(pkg, morePaths=jarPaths)
attach( javaImport( c("java.lang", "java.io", "java.mail", "java.util", "java.text")))
packageStartupMessage( paste( "mailViz loaded. The classpath is: ", paste(.jclassPath(), collapse=" " ) ) )
}
# get method arguments for the class
#.jmethods("mailReader","readEmailInfo")
z=.jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", "password")
但是,当我执行 .jcall 函数时,我收到一个错误:
Error in .jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", :
method readEmailInfo with signature (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; not found
我尝试了多种更改参数的方法,但没有成功。当我在类文件上运行 .jmethods("mailReader") 时,它列出了所有可用的方法:
[2] "public java.lang.String mailReader.readEmailInfo(java.lang.String,java.lang.String)"
因此,我不知道如何进行正确的调用,将两个参数(用户名、密码)传递给 java 类文件。
有什么想法吗?预先感谢,
P
I am currently working on developing an R package to integrate java code within R. However, I am having issues trying to properly call the java class methods. So far I have independently developed and compiled a java program into a class file and then packaged it up as a jar file. A sample of my code is as follows:
library(rJava)
.jinit()
.onLoad <- function(lib, pkg) {
pathToSdk <- paste(system.file(package = "mailViz") , "C:\\path\\to\\libs", sep="")
jarPaths <- c(paste(pathToSdk, "mail.jar", sep=""),
paste(pathToSdk, "mailReader.jar", sep="")
)
.jpackage(pkg, morePaths=jarPaths)
attach( javaImport( c("java.lang", "java.io", "java.mail", "java.util", "java.text")))
packageStartupMessage( paste( "mailViz loaded. The classpath is: ", paste(.jclassPath(), collapse=" " ) ) )
}
# get method arguments for the class
#.jmethods("mailReader","readEmailInfo")
z=.jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", "password")
However, when I execute the .jcall function I receive an error:
Error in .jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", :
method readEmailInfo with signature (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; not found
I tried multiple ways of changing the arguments but no luck. When I run .jmethods("mailReader") on the class file it lists all the methods available:
[2] "public java.lang.String mailReader.readEmailInfo(java.lang.String,java.lang.String)"
So, I am lost in how to make the proper call passing two arguments (username, password) to the java class file.
Any thoughts? Thanks in advance,
P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题,这里的关键是使用调用
,以便 R 在进行调用之前可以访问此类
。默认情况下,R 可以访问静态 java 方法。
I have resolved this issue and the key here is to use the
call so that R has access to this class before making a call in the
By default, R has access to the static java methods.