Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

JNI C++ object

Thread view: 
M. Fernandez - 28 Mar 2006 15:08 GMT
Hello,

I am working with JNI and I have a problem with my program.

In my java application there are some objects in C++ that I would like
to link them with a java object, is it possible to do?
Should I use or manage the VM?

thanks a lot,

Marcelo
MetallimaX - 28 Mar 2006 15:53 GMT
M. Fernandez a écrit :

> Hello,
>
[quoted text clipped - 7 lines]
>
> Marcelo

Hello,
I think this is possible but be carefull of using C++ libraries. If I
remember, it is the signature of the method that determine the way it
is called from an outter program.
  - Try to load a simple C library to check if your JNI is well
managed.
  - Focus on the method calling in dynamic libraries en it will help
you.

Max
M. Fernandez - 28 Mar 2006 16:28 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>
> Marcelo

I have read that it is possible to create a Java object from C++
(creating a VM). However, I would like to do it the other way around. I
need to create c++ objects from java.

What are the usual methods to do this? ()
thanks a lot,

Marcelo

PS: I just need some directions for learning by myself...
Gordon Beaton - 28 Mar 2006 17:50 GMT
> I have read that it is possible to create a Java object from C++
> (creating a VM). However, I would like to do it the other way
> around. I need to create c++ objects from java.
>
> What are the usual methods to do this? ()

The only way to do it is to add one or more native methods to a Java
class.

From one of those native methods, create your C++ objects. You cannot
create a C++ object directly from Java, nor can you access the object,
its methods or its fields directly from Java.

Read about JNI to learn how to create the necessary native methods:

 http://java.sun.com/j2se/1.5.0/docs/guide/jni/index.html
 http://java.sun.com/docs/books/jni/

/gordon

Signature

[  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

Roedy Green - 28 Mar 2006 18:26 GMT
On Tue, 28 Mar 2006 16:08:36 +0200, "M. Fernandez"
<mache_1999@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>In my java application there are some objects in C++ that I would like
>to link them with a java object, is it possible to do?
>Should I use or manage the VM?

see http://mindprod.com/jgloss/jni.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 28 Mar 2006 18:30 GMT
On Tue, 28 Mar 2006 17:26:50 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>>In my java application there are some objects in C++ that I would like
>>to link them with a java object, is it possible to do?
>>Should I use or manage the VM?
>
>see http://mindprod.com/jgloss/jni.html

In your JNI code you will need to write some glue code.  The Java
objects could be allocated by Java or by C++ with JNI. The C++ objects
are allocated by C++.  You have to write C++ code that copies fields
across or that invokes the corresponding method on the other object

The important point is that you can't do anything with C++ objects
directly from Java. You must do it indirectly via Java methods and
objects.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

M. Fernandez - 29 Mar 2006 01:26 GMT
> On Tue, 28 Mar 2006 17:26:50 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 13 lines]
> directly from Java. You must do it indirectly via Java methods and
> objects.

Thanks for your help, I have just one more question.

According to your explanation, it is impossible to have c++ objects
running at the same time as Java objects when JAVA is the main program
and has the control, for instance:

//Imposible jni program...
MAIN_JAVA
java declarations
    Creating c++ object.

more java declarations.
    using created c++ object.
continue with java.

I have understood that i should "transfer" my c++ declaration class to a
Java class in order to have java objects that may do some "native
method" calls. Is this right?

Thanks a lot,

Marcelo
Roedy Green - 29 Mar 2006 03:54 GMT
On Wed, 29 Mar 2006 02:26:54 +0200, "M. Fernandez"
<mache_1999@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>According to your explanation, it is impossible to have c++ objects
>running at the same time as Java objects when JAVA is the main program
>and has the control, for instance:
not at all. It is just that you can't manipulate or create C++ objects
DIRECTLY from Java. You need to write native method implementation
code in C++ to allow java objects and methods to indirectly create and
manipulate the C++ objects. Your c++ code has to do the object
creating an manipulating based on clues passed from Java by native
methods and parameters.

The following is not code. So I can't comment on whether it could
work.

>//Imposible jni program...
>MAIN_JAVA
[quoted text clipped - 4 lines]
>    using created c++ object.
>continue with java.
v
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

M. Fernandez - 29 Mar 2006 08:37 GMT
> On Wed, 29 Mar 2006 02:26:54 +0200, "M. Fernandez"
> <mache_1999@hotmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 22 lines]
>> continue with java.
> v
Thank your very much, this discussion was very helpful.

Marcelo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.