Creating a jobject in JNI

M

marydeepthy

I have a java method createHeader, that returns a header object.
the header object is created in a native(c)method.the native method
returns a void *, but infact i have to convert that in to a jobject and
return that jobject to the java call.
how can i do this?

JNIEXPORT jobject JNICALL getheadervalue(JNIEnv *env, jobject obj,
jlong header, jint hid)

{

void* headerValue;
headerValue=GetValue((signed short)header, (unsigned char)hID,
headerValue);
return headerValue;//have to convert from void * to jobject
}
 
J

jan V

I have a java method createHeader, that returns a header object.
the header object is created in a native(c)method.

Why?

If your method name is properly chosen, and the method does indeed create a
"header" and nothing more, then it would seem it is not
computing-intensive.. so why is it native code and not Java?
 
M

marydeepthy

actually, i am creating a java wrapper. i have to return an object of
the header in my java method.that is inevitable, i mean the
specification says so.

i am making a call from my java pgm. to an underlying c method.the c
method returns a void *.
 
M

marydeepthy

actually, i am creating a java wrapper. i have to return an object of
the header in my java method.that is inevitable, i mean the
specification says so.

i am making a call from my java pgm. to an underlying c method.the c
method returns a void *.
 
G

Gordon Beaton

I have a java method createHeader, that returns a header object.
the header object is created in a native(c)method.the native method
returns a void *, but infact i have to convert that in to a jobject and
return that jobject to the java call.
how can i do this?

JNIEXPORT jobject JNICALL getheadervalue(JNIEnv *env, jobject obj,
jlong header, jint hid)

{

void* headerValue;
headerValue=GetValue((signed short)header, (unsigned char)hID,
headerValue);
return headerValue;//have to convert from void * to jobject
}

If GetValue() returns something that is in fact already a Header
object (despite your choice of void* to hold the reference), then
simply return it.

If GetValue() returns something else that you truly need to "convert",
then this is how to create a Header object (actual conversion is up to
you):

1. Find the Header class with e.g. FindClass() or GetObjectClass().
Note that if your native method is declared static and belongs to
the Header class itself, the jobject argument passed to the method
can be used directly without looking up anything.

2. Look up the <init> method with GetMethodId().

3. Invoke <init> with NewObject(), supplying any appropriate
arguments required by the constructor.

4. Return the new object.

/gordon
 
M

marydeepthy

really sorry to trouble you all again..
i just casted the void pointer and returned it...

ie, return(jobject) headerValue. no compilation errors...will there be
any problem?
 
J

jan V

actually, i am creating a java wrapper. i have to return an object of
the header in my java method.that is inevitable, i mean the
specification says so.

The spec may say so, but that doesn't (IMO) give the developer a valid
excuse not to critically question the details of the spec. It's well known
that specs have been known to be the source of project failures, so it's
your duty as a professional to question such critical documents.

Using JNI in any project has to have a very, very, VERY good reason. "We
have a few C routines that does part of what we need" is not a good enough
reason. Years ago I worked on a project which used JNI... and that decision
caused more problems than it solved.
 
G

Gordon Beaton

really sorry to trouble you all again..
i just casted the void pointer and returned it...

ie, return(jobject) headerValue. no compilation errors...will there
be any problem?

Just test it (don't just compile it) and you'll find out sooner than
asking in Usenet.

If headerValue really is a pointer to a Java object of type Header,
then there will be no problems at all (since jobject is really just a
pointer anyway).

If headerValue is something else (for example, a pointer to a C struct
or a C++ object) then there will certainly be problems, and the JVM
will probably crash.

From the short example you posted it seems unlikely that
createHeader() actually produces a Java object, since you don't pass
the JNIEnv* pointer that it would have needed in order to do so
(although I'll admit that it could have gotten hold of that using less
direct mechanisms).

In other words, unless createHeader() was written with JNI in mind,
the job of your native method is to properly wrap that function so
that it can be called from Java, and that means *converting* the data
into a form that can be passed back to the Java caller, i.e. a Java
object.

Also, the name of your native method doesn't appear to be generated by
javah. Have actually you tested this code? Have you used JNI before?

/gordon
 
M

marydeepthy

No , i have not used JNI before. this is the first time.

But, i have created the native function name with jni.h.

JNIEXPORT jobject JNICALL Java__bluetooth_CreateHeader(JNIEnv *env,
jobject obj, jlong connectionHandle, jlong headerSetHandle)

this was the function name.

this is a big project and we have alot of files...i just compiled my c
code and it showd no errors when i did the type casting..
but now i do assume that i am wrong. i have to create an object..i am
trying to.
 
G

Gordon Beaton

this is a big project and we have alot of files...i just compiled my
c code and it showd no errors when i did the type casting.. but now
i do assume that i am wrong. i have to create an object..i am trying
to.

In my first response to you in this thread, I described the necessary
steps for creating the object.

/gordon
 
M

marydeepthy

Thanks alot. it worked.
created the object as in ur first response...
thanks a million!!!
 

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


Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top