4

In my java class I write 3 test functions as follows:

public void try2dStringArray(String[][] arr) {
    System.out.println(arr.length);

}
public void try1dStringArray(String[] arr) {
    System.out.println(arr.length);

}
public void try2dDoubleArray(double[][] arr) {
    System.out.println(arr.length);

}

In R part I run the following:

library(rJava)
.jinit('/path/to/my/jar/app.jar')
obj <- .jnew('somepackage.Someclass')
doubleMatrix <- rbind(c(1,2), c(3,4))
stringMatrix <- rbind(c('a', 'b'), c('c', 'd'))
stringArray <- c('a', 'b')

result <- .jcall(obj,"V","try1dStringArray",
                 .jarray(stringArray, dispatch = T))
result <- .jcall(obj,"V","try2dDoubleArray",
                 .jarray(doubleMatrix, dispatch = T))
result <- .jcall(obj,"V","try2dStringArray",
                 .jarray(stringMatrix, dispatch = T))

Only the last one errors out:

Error in .jcall(obj, "V", "try2dStringArray", .jarray(stringMatrix, dispatch = T)) : 
  method try2dStringArray with signature ([[Ljava.lang.String;)V not found

How is String[][] different from double[][] in this case and how can I fix it?

7
  • Does .jarray(lapply(apply(stringMatrix,1,function(x) lapply(x,.jnew,class="java/lang/String")),.jarray)) create a String[][] java object suitable for your method? Commented Mar 21, 2016 at 21:24
  • @nicola It doesn't seem to work: Error in .jcall(obj, "V", "try2dStringArray", .jarray(lapply(apply(stringMatrix, : method try2dStringArray with signature ([Ljava/lang/Object;)V not found Commented Mar 21, 2016 at 21:48
  • It seems that you got Object[][] instead of String[][]. I looked at the doc of .jarray and maybe using the argument contents.class will solve: .jarray(apply(stringMatrix,1,.jarray),contents.class="java/lang/String"). I also simplified a little the command. Hope this works. Commented Mar 21, 2016 at 22:25
  • @nicola It doesn't work neither: Error in .jcall(obj, "V", "try2dStringArray", .jarray(apply(stringMatrix, : method try2dStringArray with signature ([Ljava/lang/String;)V not found Commented Mar 21, 2016 at 22:38
  • The attempts I was making were wrong. But here is some mistery: if you try obj$try2dStringArray(.jarray(stringMatrix, dispatch = T)) it should work (at least, it works for me, printing 2). Maybe it's a bug in .jcall. Commented Mar 21, 2016 at 22:55

1 Answer 1

2

At first, I thought that .jarray wasn't able to properly create a String[][] java array from a character R matrix. I was wrong and the error received when the code in the OP is run testifies it:

.jcall(obj,"V","try2dStringArray",.jarray(stringMatrix, dispatch = T))
#Error in .jcall(obj, "V", "try2dStringArray", .jarray(stringMatrix, dispatch = T)) : 
#method try2dStringArray with signature ([[Ljava.lang.String;)V not found

As can be seen, the signature (([[Ljava.lang.String;)V) seems correct (the double [[ should say that we actually passed a String[][] object), but for some reason .jcall can't find it.

However, in rJava methods can be called also with the syntax obj$method(arg1,arg2,...), and in this way the method try2dStringArray is correctly called:

obj$try2dStringArray(.jarray(stringMatrix, dispatch = T))
#2
Sign up to request clarification or add additional context in comments.

1 Comment

So, this is a bug of rJava, isn't it? with .jcall it does work, with obj$try.. it does...

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.