Re: JNI working with jvalue (union)



Thomas Gärtner schrieb:
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.

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

.



Relevant Pages

  • Re: Typechecking union types
    ... narrow field of typechecking union types. ... In C/C++, one can, along with the union, write a struct with an enum ... A C++ polymorphic class hierarchy is viewed as a dynamic type ...
    (comp.compilers)
  • Re: Differences between C++ and Java
    ... It's probably worth mentioning struct and union. ... A lot of C/C++ ...
    (comp.lang.java.programmer)