Rjava.jcall issue

I am currently working on creating an R package to integrate Java code within R. However, I am having problems correctly calling java class methods. So far I have independently developed and compiled a java program into a class file, and then packed it 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 get an error message:

 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 several ways to change the arguments, but no luck. When I run .jmethods ("mailReader") in the class file, it lists all the available methods:

 [2] "public java.lang.String mailReader.readEmailInfo(java.lang.String,java.lang.String)" 

So, I lost the way to make the correct call passing two arguments (username, password) to the java class file.

Any thoughts? Thanks in advance,

R

+4
source share
1 answer

I solved this problem and the key here is to use

 mailReader = .jnew("mailReader") 

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

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

By default, R has access to static java methods.

+1
source

Source: https://habr.com/ru/post/1342754/


All Articles