5

Say I have a MyClass class in Java, is there a way to check in JNI that a jobject is a MyClass[][]?

My initial idea was to use env->IsInstanceOf(myobj, myArrayClass), but calling env->FindClass("[MyClass") throws a NoClassDefFoundError.

0

2 Answers 2

3

I know this question is old but...

To find a class of your array, use:

env->FindClass("[[Lmy/package/MyClass;")
Sign up to request clarification or add additional context in comments.

1 Comment

Actually that's incorrect as well, it needs the ; at the end as well (as in Rob's comment).
1

A little rusty on JNI, but a couple of things:

Call FindClass() on your fully qualified classname, using a "/" as a separator instead of dots. So, for instance if your class is "my.package.MyClass", you would call env->FindClass("my/package/MyClass")

Since you have a two-dimensional array of your object type, you need to call env->GetObjectArrayElement() twice; once to get a row and another time to get a distinct element. Then you can call env->IsInstanceOf() on that element. Make sure you look up the correct signatures for these JNI calls, I've left them as an exercise for the reader :)

3 Comments

The problem is that I don't know if the object I get is even an array, and the JNI spec doesn't even say what would happen if I try to call GetObjectArrayElement on an object that's not an array. I certainly can't rely on C++ to tell me that I can't convert jobject to jobjectArray.
You can call GetObjectClass() to get the class of the object. In your example above it would return "[LMyClass;" If the class is contained in a package, the name would be fully qualified with "/" instead of dots (i.e., "[Lmy/package/MyClass;")
Indeed, but I don't want to get the class, then get the name, then compare the strings on each call. I guess the best solution for my case would be to create an array in the first JNI call and cache its class so that I can do IsInstanceOf on it.

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.