Question

I am trying to learn jni, and i was wondering how i would make it so that a java object could have some values associated with it in the jni c++ layer. Currently i have this java code.

public class Test 
{
    static
    {
        Runtime.getRuntime().loadLibrary("JNITests");
    }

    public native void setValue(int value);
    public native int getValue();

    public static void main(String[] args) 
    {
        Test test1 = new Test();
        test1.setValue(34);
        Test test2 = new Test();
        test2.setValue(23);
        System.out.println(test1.getValue());
    }
}

So what i am trying to do is enable each Test object to store a value in the jni using setValue and get getValue, using this c++ code.

#include <jni.h>
#include "Test.h"
int value;

JNIEXPORT void JNICALL Java_Test_setValue(JNIEnv *, jobject, jint newValue)
{
value = newValue;
}

JNIEXPORT jint JNICALL Java_Test_getValue(JNIEnv *, jobject)
{
return value;
}

However, the problem is that when i use setValue on test2 and then i print test1's value it has changesd to what i set test2 to. How do i fix this. I tried mapping each jobject to an int value but that did not work either.

Was it helpful?

Solution

A possible solution may be the use of a dynamically allocated structure in the native code. For that to work, you have to allocate (for example, in the constructor) some memory that holds the structure. The address of that structure is passed back to the Java part and stored in the Java object. Then, for getValue and setValue, a parameter is added that holds the memory address of the allocated structure, you can then use for storing the value. When destroying the object, you have to free the memory manually.


Using your code, the native part may look like:

#include <cstdlib>
#include <jni.h>
#include "Test.h"

struct data {
    int value;
};

JNIEXPORT void JNICALL Java_Test_setValue0(JNIEnv *, jobject, jlong address, jint newValue)
{
    struct data *ptr = (struct data *)address;
    ptr->value = newValue;
}

JNIEXPORT jint JNICALL Java_Test_getValue0(JNIEnv *, jobject, jlong address)
{
    struct data *ptr = (struct data *)address;
    return ptr->value;
}

JNIEXPORT jlong JNICALL Java_Test_construct0(JNIEnv *, jobject) {
    struct data *ptr = (struct data *)malloc(sizeof(*ptr));
    return (jlong)ptr;
}

JNIEXPORT void JNICALL Java_Test_destruct0(JNIEnv *, jobject, jlong address) {
    struct data *ptr = (struct data *)address;
    free(ptr);
}

And the Java part would then look like:

public class Test 
{
    static
    {
        Runtime.getRuntime().loadLibrary("JNITests");
    }

    private long address;

    private native long construct0();
    private native void destruct0(long address);
    private native void setValue0(long address, int value);
    private native int getValue0(long address);

    public Test() {
        this.address = this.construct0();
    }

    @Override
    public void finalize() {
        this.destruct0(this.address);
        super.finalize();
    }

    public void setValue(int value) {
        this.setValue0(this.address, value);
    }

    public int getValue() {
        return this.getValue0(this.address);
    }

    public static void main(String[] args) 
    {
        Test test1 = new Test();
        test1.setValue(34);
        Test test2 = new Test();
        test2.setValue(23);
        System.out.println(test1.getValue());
    }
}

I renamed the native methods to introduce the address parameter without API change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top