JNI working with jvalue (union)

  • Thread starter =?ISO-8859-1?Q?Thomas_G=E4rtner?=
  • Start date
?

=?ISO-8859-1?Q?Thomas_G=E4rtner?=

Hi,

we are using Java JNI to call C++ methods from a java application. Java
is calling a get method to get data, the type of this data is variable
(int, double, string)

Is the JNI jvalue (union) datatype the right usage for this? For example:

JNIEXPORT void JNICALL getFromCppToJava(JNIEnv *env, jvalue var)
{
// i keep it very simple
var.jint = 5,
}

How is this resolved in java? How is the access to this union?

Thanks
 
T

Thomas Fritsch

Thomas said:
we are using Java JNI to call C++ methods from a java application. Java
is calling a get method to get data, the type of this data is variable
(int, double, string)

Is the JNI jvalue (union) datatype the right usage for this? For example:
No, you have to use the exact prototypes as generated by the javah-tool
from your class file (more precisely: from its methods declared as
native). Using slightly different prototypes won't work.
JNIEXPORT void JNICALL getFromCppToJava(JNIEnv *env, jvalue var)
{
// i keep it very simple
var.jint = 5,
}

How is this resolved in java? How is the access to this union?
Declare several native methods (one for int, double, String) in your
Java code. Implement the same number of C/C++ methods (with the
javah-generated prototypes). However, you may decide that all these
methods call the same C/C++ utility function, which will then do the
actual work.
 
G

Gordon Beaton

we are using Java JNI to call C++ methods from a java application.
Java is calling a get method to get data, the type of this data is
variable (int, double, string)

Is the JNI jvalue (union) datatype the right usage for this? For example:

JNIEXPORT void JNICALL getFromCppToJava(JNIEnv *env, jvalue var)
{
// i keep it very simple
var.jint = 5,
}

How is this resolved in java? How is the access to this union?

The native arguments and their types must ultimately come from the
method declaration in your java source, so you should start there.
Since jvalue doesn't correspond to a java type it isn't an option
here.

If you know in advance what type the native method will return at each
particular occasion, then a straigtforward solution is to define a
method for each of the possible return types and call the appropriate
one from Java. Then simply return the value (via the return
statement).

If you don't know in advance what type it will return, I can see a few
alternatives (there are probably other ways as well):

- define a class to hold the three types, and return an object
- assign an appropriate field in "this"
- call this.someMethod(value)

Which mechanism you choose depends on how you intend to deal with
these differently typed values in Java.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top