4

I'm writing an application where I have lot to do with JNI call and every-time I have to perform getter() call to access variable values. Instead is it possible to access Object reference of JNI object on Java Layer so can get updated variable value just by variable name (like obj.name instead obj.getName()).

I have check with this and this, but not getting way how to covert address to Object at java layer.

EDIT I wanted to access Obj this way at Java layer from JNI.

private native CustomObj getCPPCustomObjectPointer();

Any suggestion here.

13
  • 1
    You should be able to make a Java class with native methods for JNI access. Why don't you post the code you have right now so the community can take a look at it and figure out what the best way forward is? Commented Sep 29, 2015 at 9:33
  • This I followed to set object from Java-JNI but how to access from JNI-Java as an pointer Object so can access class-object variable by name. This is where I stuck. Commented Sep 29, 2015 at 9:40
  • Please post your code. Commented Sep 29, 2015 at 9:53
  • Kris, I don't know how to get object from JNI to Java, this is where I stuck. I have update what Im expecting from JNI layer. Please follow. Commented Sep 29, 2015 at 10:02
  • This might be useful: stackoverflow.com/questions/10327773/… The Oracle Java documentation for accessing object fields is here: docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/… Commented Sep 29, 2015 at 10:47

2 Answers 2

1
+100

Is it possible to access Object reference of JNI object on Java Layer?

Yes, you can. However you cannot use it for accessing its properties. You are only able to hold its address as a long value.

If you would like to do so, you should create your C++ objects in heap memory and return their addresses as long numbers.

MyClass *obj = new MyClass();
return (long) obj;

In Java side you can save that address as a long number wherever you want. Since objects have been created in heap memory, they will remain valid between JNI calls.

Also, you have to pass them to later JNI calls as a long number and then you should cast them to MyClass * in your C++ side.

MyClass *obj = (MyClass *)thatLongNumber;
obj->someProperty; // Access its properties and methods via -> operator
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks..about to hold address as long, I'm aware but as no use if can not access property. Can we have some pseudo code to access property via JNI-> Java -> JNI(access property).
I couldn't understand what you mean by "JNI-> Java -> JNI(access property)". If you would want to access properties/fields of a C++ object (which its address as a long number is already known at Java side), AFAIK, you cannot do this at Java side but you can do at C++ side.
I mean ..get address from JNI to JAVA and Now take same address from Java to JNI to read object property as pointer reference. I'm stuck how to create object via this 'long' address at JNI layer.
@Shubh Simply by casting it. MyClass *obj = (MyClass *)thatLongNumber;
0

You want to retain a reference to a C++ object from your Java side? you can't.

Those implementations (C/Java) for representing and accessing objects/primitives are completely different. That's why there's so much mambo jambo functions when you you cast from one data type to another.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.