Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rjava .jcall issue

Tags:

r

rjava

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

like image 530
prao Avatar asked Nov 14 '22 02:11

prao


1 Answers

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.

like image 86
prao Avatar answered Jan 01 '23 15:01

prao