JNI C++ object

M

M. Fernandez

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
 
M

MetallimaX

M. Fernandez a écrit :
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


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

M. Fernandez

M. Fernandez said:
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

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...
 
R

Roedy Green


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.
 
G

Gordon Beaton

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
 
M

M. Fernandez

Roedy said:
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.

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
 
R

Roedy Green

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
java declarations
Creating c++ object.

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

M. Fernandez

Roedy said:
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.

v
Thank your very much, this discussion was very helpful.

Marcelo
 

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

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top