rjava .jcall 问题

发布于 2024-10-21 01:53:48 字数 1370 浏览 5 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-10-28 01:53:48

我已经解决了这个问题,这里的关键是使用调用

mailReader =    .jnew("mailReader")

,以便 R 在进行调用之前可以访问此类

z = .jcall(mailReader, "S", etc....)  

。默认情况下,R 可以访问静态 java 方法。

I have resolved this issue and the key here is to use the

mailReader =    .jnew("mailReader")

call so that R has access to this class before making a call in the

z = .jcall(mailReader, "S", etc....)  

By default, R has access to the static java methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文