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!

1 Answer 1

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;.

Sign up to request clarification or add additional context in comments.

2 Comments

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: Great to hear you got it working! :) In future, remember you can 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.