Search code examples
javarrjava

rJava .jnew java.lang.NoSuchMethodError


I am new to 'rJava' and meet some troubles for hours.

library(rJava)
.jinit(classpath="C:\\Users\\XXX\\desktop\\example")
.jclassPath()
# [1] "C:\\Users\\XXX\\Documents\\R\\win-library\\3.5\\rJava\\java"          
# [2] "C:\\Users\\XXX\\desktop\\example" 
s=.jnew("java/lang/String", "Hello World")
.jcall(s, "I", "length")
# 11
.jcall('Exampletest', returnSig='I', method='test')
# 1
.jnew('Exampletest', 3)
# Error in .jnew("Exampletest", 3) : java.lang.NoSuchMethodError: <init>

This is quite weired since I have successfully call the function 'test' but still fail to new the object.

The java code is here

public class Exampletest {

    static public int test() {
        return 1;
    }

    public Exampletest(int x) {
        this.x = x;
    }

    private int x;
}

Any idea is appreciated!


Solution

  • Aha, I have met this problem last year when I begin to use rJava. This problem is simply because in r, 3 is a numeric number but not an integer. So you should use 3L instead of 3

    BTW, if the arguments have the long type in java, 3L is not working again. You should use .jlong(3) instead of 3L again. The signature of the function is quite important in rJava!