Search code examples
javacjava-native-interface

CallXXXMethod undefined using JNI in C


So I've tried to use the JNI interface to call Java methods from C. Calling static methods is no problem, but I get stuck when I want to call a method on an object. The code is as follows:

#include <stdio.h>
#include <string.h>

#include <jni.h>

int main()
{
    JavaVMOption options[1];
    JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jclass cls, cls2;
    jmethodID mid;
    jint square;
    jboolean not;
    jobject person;

    options[0].optionString ="-Djava.class.path=.";
    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 1;
    vm_args.options = options;
    status = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

    if (status != JNI_ERR) {
        cls = (*env)->FindClass(env, "Test");
        if (cls != 0) {
            mid = (*env)->GetStaticMethodID(env, cls, "square", "(I)I");
            if (mid != 0) {
                square = (*env)->CallStaticIntMethod(env, cls, mid, 7);
                printf("Square: %i\n", square);
            }

            /*mid = (*env)->GetStaticMethodID(env, cls, "newWindow", "()V");
            if (mid) {
                (*env)->CallStaticVoidMethod(env, cls, mid, NULL);
            }*/

            mid = (*env)->GetStaticMethodID(env, cls, "newPerson", "(ILjava/lang/String;)LPerson;");
            if (mid) {
                person = (*env)->CallStaticObjectMethod(env, cls, mid, 23, "Nox");
                cls2 = (*env)->FindClass(env, "Person");
                mid = (*env)->GetMethodID(env, cls2, "getAge", "()I");
                jint age = (*env)->CallIntMethod(env, cls2, mid, NULL);
                printf("age: %i\n", age);
            }
        }
        (*jvm)->DestroyJavaVM(jvm);
        return 0;
    }

}

The method I want to call is "getAge" from a Person object. The Person object is returned from the main class Test method "newPerson". When I run the program, I get the following error:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00002b2a136b8e00, pid=12928, tid=47459705508864
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b17) (build 1.8.0_25-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x6a9e00]  jni_invoke_nonstatic(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x1c0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/noxet/pt/c/jni/hs_err_pid12928.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#
Aborted
make: *** [run] Error 134

My Java code is quite simple:

class Person {
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public String getName() {
        return this.name;
    }
}

class Test {
    public static int square(int n) {
        return n * n;
    }

    public static boolean not(boolean bool) {
        return !bool;
    }

    public static Person newPerson(int age, String name) {
        return new Person(age, name);
    }
}

Solution

  • There are few fixes required in the code:

    1. CallIntMethod should be (*env)->CallIntMethod
    2. class Test should be public
    3. Invocation should be jint age = (*env)->CallIntMethod(env, mod_obj, mid, NULL);

    Note that you need class name to call a static function but an object to call a method. (cls2 -> person)

    mid = (*env)->GetStaticMethodID(env, cls, "newPerson", "(ILjava/lang/String;)LPerson;");
    mod_obj = mid;
    if (mid) {
        person = (*env)->CallStaticObjectMethod(env, cls, mid, 23, "Nox");
        cls2 = (*env)->FindClass(env, "Person");
        mid = (*env)->GetMethodID(env, cls2, "getAge", "()I");
        jint age = (*env)->CallIntMethod(env, mid_obj, mid, NULL);
        printf("age: %i\n", age);
    }