timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
166 stars 44 forks source link

NoSuchMethodError #38

Closed unlimitedcoder2 closed 4 years ago

unlimitedcoder2 commented 4 years ago

img

err = jnigi.LoadJVMLib(jnigi.AttemptToFindJVMLibPath());

if err != nil {
    fmt.Println(err);
    return;
}

opts := jnigi.NewJVMInitArgs(false, true, jnigi.JNI_VERSION_1_8, []string{"-Djava.class.path=java/lire.jar:java/liresolr.jar"});

_, env, err := jnigi.CreateJVM(opts);

if err != nil {
    fmt.Println(err);
    return;
}

data, err := env.CallStaticMethod(
    "net/semanticmetadata/lire/solr/indexing/ParallelSolrIndexer",
    "hashImage",
    jnigi.ObjectType("java/lang/String"),
    env.NewByteArrayFromSlice(frame),
    "test",
    "123");

if err != nil {
    fmt.Println(err);
    return;
}

fmt.Println(data);

I know the classpath is correct and it detects the class but it cannot find my static method. any idea as to why it cant find my method?

timob commented 4 years ago

It's because JNIGI only converts primitive types between Go and Java . So the last two arguments to hashImage need to be String objects, these must be created by the app. See https://github.com/timob/jnigi/blob/master/jnigi_test.go#L107 for an example of this. Basically you bass a []byte to the String class constructor to create a String object which you can then pass to the method.

But this is still a bug in JNIGI, because it should return an error to indicate it doesn't know how to handle a type passed in an argument. Right now it just incorrectly creates the method signature and the returns NoSuchMethodError.