Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI return jobjectArray

Reply
Thread Tools

JNI return jobjectArray

 
 
Philipp Kraus
Guest
Posts: n/a
 
      09-30-2012
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

 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      09-30-2012
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>

 
Reply With Quote
 
 
 
 
Philipp Kraus
Guest
Posts: n/a
 
      09-30-2012
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


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      10-01-2012
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.




 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-01-2012
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
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      10-01-2012
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.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-01-2012
On Sun, 30 Sep 2012 22:23:48 +0200, Philipp Kraus
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Steven Simpson
Guest
Posts: n/a
 
      10-01-2012
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/DoubleV
*/
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments