0

I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.

Here is an extract of the java code for which I write a wrapper:

public class Model4R{

[...cut...]

public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {

[...cut...]

dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.

Here is my tentative R code for a wrapper function:

runmodel <- function(dir, inFile, varNames, numSim){

hjw <- .jnew("Model4R")

out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))

return(out)

}

The error in R is:

Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found

I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!

franchong
  • 1
  • 1

1 Answers1

1

You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.

Lizzie Silver
  • 293
  • 3
  • 13
  • I've solved it in the meantime with help from the rJava author, but thanks, indeed these were exactly the points that I was missing! – franchong Oct 23 '15 at 06:51
  • @franchong: Great to hear you got it working! :) In future, remember you can [answer your own questions](https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). This helps other users with the same problem, and stops potential answerers from duplicating your effort. Welcome to Stack Overflow - I hope you enjoy the site! – Lizzie Silver Oct 23 '15 at 14:43