Search code examples
rjava

rJava .jcall return type issue


I am facing an issue with returning any type of array with .jcall(). Here is my code.

public class Test(){
  public static double[] sample(){

  double[] nobjarr = new double[5]
  nobjarr[0] = 1.0;
  nobjarr[1] = 1.0;
  nobjarr[2] = 1.0;
  nobjarr[3] = 1.0;
  nobjarr[4] = 1.0;

  return nobjarr;
}

}

In R, I am calling using .jcall

library(rJava)                          
.jinit()   
.jaddClassPath("path to .class file")    
objT <- .jnew("Test")    
res  <- .jcall(objT,"[D","sample")

For this I get an error saying "Error in .jcall(objT, "[D", "sample") :method sample with signature ()[D not found"


Solution

  • Have you tried something like this:

    Test <- J( "Test" )
    Test$sample()
    

    This uses the reflection based API that is in rJava for several years now and is much more convenient than the low level .jnew, .jcall interface.