Solved

JNI QUESTION

Posted on 1998-05-04
5
1,127 Views
Last Modified: 2013-11-23
Hi,
I have a JAVA method that returns a int[2].
How can I call this method in JNI? Is  there something similar to CallStaticIntMethod?
Thanks.
0
Comment
Question by:jjescri
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
ID: 1220498
This is from JNI documentation on CallStatic<Type>Method:
__________________________________________________
CallStatic<type>Method Routines
CallStatic<type>MethodA Routines
CallStatic<type>MethodV Routines

NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);

NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);

NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz,
jmethodID methodID, va_list args);

This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling
GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.

CallStatic<type>Method Routines

Programmers should place all arguments that are to be passed to the method immediately following the methodID argument. The CallStatic<type>Method
routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.

CallStatic<type>MethodA Routines

Programmers should place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The CallStaticMethodA
routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

CallStatic<type>MethodV Routines

Programmers should place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The
CallStaticMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

The following table describes each of the method calling routines according to their result types. You should replace type in CallStatic<type>Method with the
Java type of the method, or one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that
routine.

                             Table 4-7 CallStatic<type>Method Calling Routines

                             CallStatic<type>Method Routine Name
                                                                                      Native Type
      CallStaticVoidMethod() CallStaticVoidMethodA() CallStaticVoidMethodV()
                                                                                      void
      CallStaticObjectMethod() CallStaticObjectMethodA() CallStaticObjectMethodV()
                                                                                      jobject
      CallStaticBooleanMethod() CallStaticBooleanMethodA() CallStaticBooleanMethodV()
                                                                                      jboolean
      CallStaticByteMethod() CallStaticByteMethodA() CallStaticByteMethodV()
                                                                                      jbyte
      CallStaticCharMethod() CallStaticCharMethodA() CallStaticCharMethodV()
                                                                                      jchar
      CallStaticShortMethod() CallStaticShortMethodA() CallStaticShortMethodV()
                                                                                      jshort
      CallStaticIntMethod() CallStaticIntMethodA() CallStaticIntMethodV()
                                                                                      jint
      CallStaticLongMethod() CallStaticLongMethodA() CallStaticLongMethodV()
                                                                                      jlong
      CallStaticFloatMethod() CallStaticFloatMethodA() CallStaticFloatMethodV()
                                                                                      jfloat
      CallStaticDoubleMethod() CallStaticDoubleMethodA() CallStaticDoubleMethodV()
                                                                                      jdouble


PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.

Additional Parameter for CallStatic<type>Method Routines:

arguments to the static method.

Additional Parameter for CallStatic<type>MethodA Routines:

args: an array of arguments.

Additional Parameter for CallStatic<type>MethodV Routines:

args: a va_list of arguments.

RETURNS:

Returns the result of calling the static Java method.

THROWS:

Exceptions raised during the execution of the Java method.
__________________________________________________

I think you need CallStaticObejctMethod function (rateher than CallStaticIntMethod).

When you get an array, you can use Get<PrimitiveType>ArrayElements routines:

__________________________________________________
Get<PrimitiveType>ArrayElements Routines

NativeType *Get<PrimitiveType>ArrayElements(JNIEnv *env,
ArrayType array, jboolean *isCopy);

A family of functions that returns the body of the primitive array. The result is valid until the corresponding Release<PrimitiveType>ArrayElements() function is
called. Since the returned array may be a copy of the Java array, changes made to the returned array will not necessarily be reflected in the original
array until Release<PrimitiveType>ArrayElements() is called.

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.

The following table describes the specific primitive array element accessors. You should make the following substitutions:

     Replace Get<PrimitiveType>ArrayElements with one of the actual primitive element accessor routine names from the table.
     Replace ArrayType with the corresponding array type.
     Replace NativeType with the corresponding native type for that routine.

Regardless of how boolean arrays are represented in the Java VM, GetBooleanArrayElements() always returns a pointer to jbooleans, with each byte
denoting an element (the unpacked representation). All arrays of other types are guaranteed to be contiguous in memory.

         Table 4-9 Get<PrimitiveType>ArrayElements Family of Accessor
                                 Routines

      Get<PrimitiveType>ArrayElements Routines
                                             Array Type
                                                        Native Type
      GetBooleanArrayElements()
                                             jbooleanArray
                                                        jboolean
      GetByteArrayElements()
                                             jbyteArray
                                                        jbyte
      GetCharArrayElements()
                                             jcharArray
                                                        jchar
      GetShortArrayElements()
                                             jshortArray
                                                        jshort
      GetIntArrayElements()
                                             jintArray
                                                        jint
      GetLongArrayElements()
                                             jlongArray
                                                        jlong
      GetFloatArrayElements()
                                             jfloatArray
                                                        jfloat
      GetDoubleArrayElements()
                                             jdoubleArray
                                                        jdouble


PARAMETERS:

env: the JNI interface pointer.

array: a Java string object.

isCopy: a pointer to a boolean.

RETURNS:

Returns a pointer to the array elements, or NULL if the operation fails.


0
 

Author Comment

by:jjescri
ID: 1220499
What I ask is a detailed example for an array of 2 integers.
Thanks.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1220500
// get class pointer
jclass myClass = (*env)->FindClass(env, "myPackage.MyClass");

// get method id
//    Ex.: "(Ljava/lang/String;)[I" - signature of method taking
//      String and returning an int[].
jmethodID methodId = GetMethodID(env, myClass, "methodName", "methodSignature");

// Get the Java array object
jobject retVal = CallStaticObjectMethod(env, myClass, methodId);

// Convert Java array to C array
jint[] cArray = GetIntArrayElements(env, (jintArray)retval, NULL);
0
 

Author Comment

by:jjescri
ID: 1220501
msmolyak,

last three lines should be:


 jmethodID methodId =(*env)-> GetMethodID(env, myClass, "methodName", "methodSignature");

jobject retVal =(*env)-> CallStaticObjectMethod(env, myClass, methodId);

jint[] cArray = (*env)->GetIntArrayElements(env, (jintArray)retval, NULL);

but in last line the compiler returns ERROR:'=' : left operand must be l-value
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1220502
Thanks for correction. Apparently mine was not a complied code.

Try jint* instead of jint[]. (In C you get the pointer to the first array element).
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 49
changePi Challenge 15 77
HashMap Vs TreeMap 12 49
Requested array size exceeds VM limit 3 51
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now