Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI: instace object C++ from Java

Reply
Thread Tools

JNI: instace object C++ from Java

 
 
flack
Guest
Posts: n/a
 
      03-21-2006
I'm a newby about JNI.
I have an application write in C++ and I have to migrate it to Java.
I have seen that JNI permit to call c++ methods that return only
primitive type as string or int.
My question is simple: if I have a C++ class that we call "ClassA", can
I instance an object "ClassA" in Java that run the constructor and
where can I call the methods of "ClassA" as I was in C++?

I want something like this in Java:

jClassA a;
jClassB b
b = h.methodClassA(int d);


Can I do that?

Thank you!

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 07:07:46 -0800, flack wrote:
> I have an application write in C++ and I have to migrate it to Java.
> I have seen that JNI permit to call c++ methods that return only
> primitive type as string or int.


Not just primitives: you can pass *any* Java types (primitives or
Objects) between Java and native code. Please note that String is not
a primitive.


> My question is simple: if I have a C++ class that we call "ClassA",
> can I instance an object "ClassA" in Java that run the constructor
> and where can I call the methods of "ClassA" as I was in C++?


No. JNI doesn't let you invoke arbitrary methods written in C or C++,
or create C++ objects.

JNI is a mechanism that lets you implement some (or all) of a Java
class's methods in C or C++. Furthermore those methods must have been
written with the intent of being called through JNI, i.e. they must
use signatures and data types as defined by JNI, and follow a specific
calling convention.

However from those native methods you are free to create C++ objects
and invoke their methods however you like.

JNI also lets your native code invoke any Java methods or
constructors, or access fields in Java objects.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
flack
Guest
Posts: n/a
 
      03-21-2006
Are you sure that there isn't some trick to use c++ methods?

The doubt is coming when I have read this post:
http://groups.google.it/group/comp.l...c2b31639123573

What do you think about that?

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 09:49:18 -0800, flack wrote:
> Are you sure that there isn't some trick to use c++ methods?


Did you actually read the thread you referred to?

The example shows that it is possible to call a native method, and
_from_there_ create the C++ object. If you read my earlier answer,
you'll see that this is exactly what I said you must do.

The only "trick" here is that the C++ pointer is passed back to Java
as an int (although I would recommend a long).

You still can't use the object from Java. To do anything meaningful
with it you must first pass it back to a native method, then cast it
to whatever object it once was.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 07:07:46 -0800, "flack" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>jClassA a;
>jClassB b
>b = h.methodClassA(int d);
>


Normally, Java calls C/C++, but you can do the reverse. Normally you
just pass primitives back and forth. On the C++ side, you can create a
Java object with the JNI API and populate its fields and return it,
most commonly a String.

You can also create C++ objects and use them on the C++ side. They
mean nothing on the Java side so you can't bring them over into Java
without converting them to Java objects first.


see http://mindprod.com/jgloss/jni.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 16:28:45 +0100, Gordon Beaton <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>No. JNI doesn't let you invoke arbitrary methods written in C or C++,
>or create C++ objects.


More precisely, from Java you cannot create arbitrary C or C++
objects, but of course you can create them on the C++ side.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      03-21-2006
On Tue, 21 Mar 2006 18:20:19 GMT, Roedy Green wrote:
> More precisely, from Java you cannot create arbitrary C or C++
> objects, but of course you can create them on the C++ side.


Which is exactly what I wrote a couple of sentences later.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 20:02:08 +0100, Gordon Beaton <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>Which is exactly what I wrote a couple of sentences later.


I thought your explanation was going over OP's head. So I decided to
restate it in a more elementary way.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
unodivoi
Guest
Posts: n/a
 
      03-27-2006
First, thanks to all for response.

I haven't understand how I can manage the object pointer.
Can someone do an explicit example?
We can suppose that we have in java this code:

public class JObject {

private final long objectPtr = 0;
private native void objectCreate();

public static void main(String[] args){
JObject my = new JObject();
my.objectCreate();
}
}



In native code what will we write?

JNIEXPORT void JNICALL
Java_objectManager_objectCreate(JNIEnv *env, jobject obj)
{

CppObject *ptr = 0;
CppObject myCppObject;
ptr = &myCppObject;

??????????????????

}


Can someone rewrite correctly the above code with the target to
instance a C++ class (CppObject) and return to Java its pointer?

Thank you!!!!!

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      03-27-2006
On 26 Mar 2006 17:19:39 -0800, unodivoi wrote:
> Can someone rewrite correctly the above code with the target to
> instance a C++ class (CppObject) and return to Java its pointer?


It's trivial if you declare the native method so that it *returns* the
long:

private native long objectCreate();

Then:

return (jlong)&myCppObject;

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
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
Missing @ for instace variable in class, but no error message Stefan Salewski Ruby 5 10-07-2010 03:47 AM
Object test = new Object() <-- Java, best way in C++ DrUg13 C++ 13 09-30-2008 04:20 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
check instace already running... Fabio Pliger Python 3 04-10-2005 02:10 PM
Transform Java object to XML string object Matt Java 7 05-19-2004 05:20 AM



Advertisments