javarrjava

rJava .jcall java method parameters input issue: with signature ([D[DDD)Ljava/lang/String; not found


Here is my original java code with method of "performCurveFitting" method

public static String performCurveFitting(Double[] conc, Double[] resp, Double classificationSD, Double minYrange) {
        CurveFittingService fittingService = new CurveFittingServiceImpl();
        FittingResult fittingResult = fittingService.doFit(conc, resp, classificationSD, minYrange);
        
        // Convert the fitting result to a String representation
        return fittingResult.toString(); // Assuming FittingResult has overridden toString() method
    }

In R code,

I did the following commands with no errors

.jinit('.')
.jaddClassPath('inst/java/curve-fitting.jar')
curvefit <- .jnew('gov/nih/ncats/ifx/qhts/curvefitting/CurveClassFit')
.DollarNames(curvefit)

However, when I tested the method using the following command

.jcall(curvefit, returnSig = "Ljava/lang/String;", method = "performCurveFitting", c(5.5707525685E-10, 4.5123095805E-8, 1.2183235867E-6, 3.2894736842E-5),c(2,4,6,100), 5, 10)

I got this error

Error in .jcall(curvefit, returnSig = "Ljava/lang/String;", method = "performCurveFitting", : method performCurveFitting with signature ([D[DDD)Ljava/lang/String; not found

How can I fix this error?

Thank you!


Solution

  • The reason here is that your code looks for a method with double and you are defining it with Double

    > javap -s  Simple
    Compiled from "Simple.java"
    public class Simple {
      public Simple();
        descriptor: ()V
    
      public static java.lang.String performCurveFitting(java.lang.Double[], java.lang.Double[], java.lang.Double, java.lang.Double);
        descriptor: ([Ljava/lang/Double;[Ljava/lang/Double;Ljava/lang/Double;Ljava/lang/Double;)Ljava/lang/String;
    
      public static java.lang.String performCurveFitting2(double[], double[], double, double);
        descriptor: ([D[DDD)Ljava/lang/String;
    }
    }
    

    Try using double instead. Should be visible to your R code.

    With javap command you can see the exact signature of your code. As you can see. Your signature is:

    [Ljava/lang/Double;[Ljava/lang/Double;Ljava/lang/Double;Ljava/lang/Double;)Ljava/lang/String;
    

    while your R code is looking for:

    [D[DDD)Ljava/lang/String; 
    

    Most probably, R uses small Java's doubles whenever you are using R doubles passed to Java.