Search code examples
rrjava

Passing parameters in .jcall


I have just started working with rJava to utilise a host of Java code in an R based application. I've tried some simple "Hello world" type things so I know the basic setup is working. I have several issues however I am hoping they will be resolved if I can resolve this basic problem using .jcall.

> cal = new(J("java/util/GregorianCalendar"))
> obj = new(J("au.gov.ips.dataarchive.TIndex"))
> obj$monthlyT(cal)
[1] 77
> .jcall(obj,"I","monthlyT",cal)
    Error in .jcall(obj, "I", "monthlyT", cal) : 
     method monthlyT with signature (Ljava/util/GregorianCalendar;)I not found

To my understanding, the 3rd and 4th lines are equivalent and should produce the same result. Clearly I am doing something wrong. The 'monthlyT' method is defined in the java code as:

static public Integer monthlyT(Calendar month)

I am not a Java expert, so please let me know what other info about the Java objects I might need to provide to answer the question.


Solution

  • cal is a java.util.GregorianCalendar and not a java.util.Calendar. If you want to use the low-level .jcall interface (why?) then you need to do the casting yourself. So something like this:

    .jcall(obj,"I","monthlyT",.jcast(cal, "java/util/Calendar" ))