JNI return jobjectArray

Discussion in 'Java' started by Philipp Kraus, Sep 30, 2012.

  1. Hello,

    I hope this question is not OT.

    I create a JNI call for this Java method

    class myclass {

    void native mymethod( Double[] x )
    }

    so the parameter x should be a call-by-reference, I would set x in the
    native JNI function:

    void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
    {
    // do something
    jobjectArray t = .....

    jarg = t;
    }

    How can I set the jobjectArray& back, so the data is return in the parameter x?
    IMHO I need jenv->NewGlobalRef, os this the correct use jarg =
    (jobjectArray)jenv->NewGlobalRef( (jobject*) &t ) ?

    Thanks

    Phil
    Philipp Kraus, Sep 30, 2012
    #1
    1. Advertising

  2. Philipp Kraus

    markspace Guest

    On 9/30/2012 1:23 PM, Philipp Kraus wrote:
    > Hello,
    >
    > I hope this question is not OT.
    >
    > I create a JNI call for this Java method
    >
    > class myclass {
    >
    > void native mymethod( Double[] x )
    > }
    >
    > so the parameter x should be a call-by-reference, I would set x in the
    > native JNI function:
    >
    > void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
    > {
    > // do something
    > jobjectArray t = .....
    >
    > jarg = t;
    > }
    >
    > How can I set the jobjectArray& back, so the data is return in the
    > parameter x?



    You can't. Java doesn't have a pass-by-reference call scheme, at all.
    Java uses only pass by value. You could:

    1. Return the new array as a return value.
    2. Create a reference to a reference so you can replace the 2nd one:

    > class myclass {
    >
    > void native mymethod( Double[][] x )
    > }


    will give you a pointer to an array which you can then modify.

    A Google search for "Java pass by reference" will give you lots of
    details. Maybe add "jni" to get some clues more specific results for
    your particular situation.

    <https://www.google.com/search?q=java+pass+by+reference>
    markspace, Sep 30, 2012
    #2
    1. Advertising

  3. On 2012-09-30 23:56:57 +0200, markspace said:

    > On 9/30/2012 1:23 PM, Philipp Kraus wrote:
    >> Hello,
    >>
    >> I hope this question is not OT.
    >>
    >> I create a JNI call for this Java method
    >>
    >> class myclass {
    >>
    >> void native mymethod( Double[] x )
    >> }
    >>
    >> so the parameter x should be a call-by-reference, I would set x in the
    >> native JNI function:
    >>
    >> void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
    >> {
    >> // do something
    >> jobjectArray t = .....
    >>
    >> jarg = t;
    >> }
    >>
    >> How can I set the jobjectArray& back, so the data is return in the
    >> parameter x?

    >
    >
    > You can't. Java doesn't have a pass-by-reference call scheme, at all.
    > Java uses only pass by value. You could:
    >
    > 1. Return the new array as a return value.
    > 2. Create a reference to a reference so you can replace the 2nd one:
    >
    > > class myclass {
    > >
    > > void native mymethod( Double[][] x )
    > > }

    >
    > will give you a pointer to an array which you can then modify.
    >
    > A Google search for "Java pass by reference" will give you lots of
    > details. Maybe add "jni" to get some clues more specific results for
    > your particular situation.


    You are not right, because the problem is not the Java side, it is the
    JNI (C/C++) side, so under C/C++ exists call-by-reference.

    In detail I have got a native (C) methode, that shows in Java:

    class myclass {
    native void mymethod( Double[] x, Double[][] y )
    }

    The JNI glue code generates a

    void mymethod (JNIEnv* jnv, jclass jcls, jobjectArray arg1, jobjectArray arg2 )

    So the reference to the underlaying heap object is passed in the JNI
    code parameters arg1 & arg2,
    so I would modify this two heap adresses.

    If I call in Java this code:

    Double[] x = null;
    Double[][] y = null;

    myclass.mymethod(x, y);
    // x & y are now filled with data


    in x & y should be create a new array object, so I need a
    GlobalReference on the heap of the VM
    and push back the reference. I can create the global object on the VM
    heap, but how can I push back
    the reference
    Philipp Kraus, Sep 30, 2012
    #3
  4. Philipp Kraus

    markspace Guest

    On 9/30/2012 3:23 PM, Philipp Kraus wrote:

    > You are not right,



    I'm not right? And yet...


    > heap, but how can I push back
    > the reference



    ....the problem still exists.


    > In detail I have got a native (C) methode, that shows in Java:



    Truthfully, there is not enough detail here for me to guess what the
    problem really is. You're showing method signatures but no code.


    > If I call in Java this code:
    >
    > Double[] x = null;
    > Double[][] y = null;
    >
    > myclass.mymethod(x, y);



    No this will not work. I guess I was not specific enough: *you* have to
    create a reference to the array reference you want to modify. That
    doesn't happen if the parameter is null.

    Double x = { {1.2} };

    Now you have something to modify. Java does NOT have pass by reference,
    you must do it yourself. I did a Google search, and I didn't see the
    solution, so here I guess is some lost knowledge. This is Java, you'll
    have to translate to C++ on your own:

    class Example {

    // manual "pass by reference"
    void makeNewDoubleArray( Double [][] x ) {
    x[0] = new Double[] { 1.1, 2.2, 3.3 };
    }

    public static void main( String... args ) {
    Double[] y = {0.0};
    Double[][] wrapper = { {} };
    wrapper[0] = y; // pack
    makeNewDoubleArray( wrapper );
    y = wrapper[0] // unpack
    System.out.println( java.util.Arrays.deepToString( y ) );
    }
    }

    Code is untested; watch out for silly errors.
    markspace, Oct 1, 2012
    #4
  5. Philipp Kraus

    Lew Guest

    markspace wrote:
    > Philipp Kraus wrote:
    >> You are not right,

    >
    > I'm not right? And yet...
    >
    >> heap, but how can I push back the reference

    >
    > ...the problem still exists.
    >
    >> In detail I have got a native (C) methode, that shows in Java:

    >
    > Truthfully, there is not enough detail here for me to guess what the
    > problem really is. You're showing method signatures but no code.


    What he did show was a parameter passed from Java as a C++ reference.

    Does that even work? Java doesn't have anything like C++ references to pass.

    That being the advice the OP blew off immediately.

    >> If I call in Java this code:
    >>
    >> Double[] x = null;


    'x' is equivalent to a pointer, not a reference.

    Pardon my ignorance, but what if you used a pointer in the native code
    instead of a reference?

    > > Double[][] y = null;
    >>
    >> myclass.mymethod(x, y);


    Java convention calls for type names to start with an upper-case letter
    and use camel case.

    > No this will not work. I guess I was not specific enough: *you* have to
    > create a reference to the array reference you want to modify. That


    Well, in C++ terms, wouldn't that be a pointer?

    > doesn't happen if the parameter is null.
    >
    > Double x = { {1.2} };
    >
    > Now you have something to modify. Java does NOT have pass by reference,
    > you must do it yourself. I did a Google search, and I didn't see the
    > solution, so here I guess is some lost knowledge. This is Java, you'll
    > have to translate to C++ on your own:
    >
    > class Example {
    >
    > // manual "pass by reference"
    >
    > void makeNewDoubleArray( Double [][] x ) {
    > x[0] = new Double[] { 1.1, 2.2, 3.3 };
    > }
    >
    > public static void main( String... args ) {
    > Double[] y = {0.0};
    > Double[][] wrapper = { {} };
    > wrapper[0] = y; // pack
    > makeNewDoubleArray( wrapper );
    > y = wrapper[0] // unpack
    > System.out.println( java.util.Arrays.deepToString( y ) );
    > }
    > }
    >
    > Code is untested; watch out for silly errors.


    I am ignorant of the ways of JNI, so my question might be extraordinarily
    off base.

    --
    Lew
    Lew, Oct 1, 2012
    #5
  6. Philipp Kraus

    markspace Guest

    On 9/30/2012 9:14 PM, Lew wrote:

    > I am ignorant of the ways of JNI, so my question might be extraordinarily
    > off base.
    >



    I'm pretty ignorant of JNI also, but I'm making the assumption that
    Chris Upal is correct (he posted after you and I; I was just making an
    educated guess). It *must* work the same was as native Java, since
    there's no other way for Java to handle it.

    The OP is just confused about reference vs value, and hoping Java has an
    "out." It doesn't, and he'll have to adjust his thinking.
    markspace, Oct 1, 2012
    #6
  7. Philipp Kraus

    Roedy Green Guest

    On Sun, 30 Sep 2012 22:23:48 +0200, Philipp Kraus
    <> wrote, quoted or indirectly quoted
    someone who said :
    Set up your glue methods in Java and mark them native.

    Then use javah to create your c stubs. The write your C/C++ methods.

    See http://mindprod.com/jgloss/jni.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The iPhone 5 is a low end Rolex.
    Roedy Green, Oct 1, 2012
    #7
  8. On 30/09/12 21:23, Philipp Kraus wrote:
    > class myclass {
    >
    > void native mymethod( Double[] x )
    > }


    I realise this snippet could be just for illustration, but 'native' must
    come before the return type in real code. Missing semicolon too.

    > so the parameter x should be a call-by-reference, I would set x in the
    > native JNI function:
    >
    > void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )


    When I tried the class (after the fixes) with javah, I got this:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class myclass */

    #ifndef _Included_myclass
    #define _Included_myclass
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class: myclass
    * Method: mymethod
    * Signature: ([Ljava/lang/Double;)V
    */
    JNIEXPORT void JNICALL Java_myclass_mymethod
    (JNIEnv *, jobject, jobjectArray);

    #ifdef __cplusplus
    }
    #endif
    #endif

    The jobjectArray parameter has no '&'. If it had one, it would not be
    possible to link it with a C implementation. If an '&' is inserted
    manually, and the function is implemented in C++, the machine code would
    likely be identical to an equivalent function using a pointer, but the
    JVM would not pass a jobjectArray* to it, and you'd get undefined behaviour.


    --
    ss at comp dot lancs dot ac dot uk
    Steven Simpson, Oct 1, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    835
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    890
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,661
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    607
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    785
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page