Creating a C/C++ library using Java

Bryce McKinlay bryce@waitaki.otago.ac.nz
Mon Aug 5 19:17:00 GMT 2002


Juan Altmayer Pizzorno wrote:

>has anybody thought about creating a C/C++ library using Java and gcj?
>
Do you mean creating a library which can be called from a C/C++ 
application? If so then yes, its quite possible using the CNI or JNI 
invocation APIs - see http://gcc.gnu.org/onlinedocs/gcj/Invocation.html 
for details.

>I haven't looked into the internals closely yet, but it seems to me that at
>least the garbage collector would make it difficult (right?).  I am not
>sure whether there would be actual conflicts, or whether the library would
>just behave in an unexpected way (e.g., seem to initiallly leak memory).
>
In general there shouldn't be any problems with the GC. It should be 
able to co-exist peacefully with native code that uses malloc, etc. One 
thing to be aware of is that you shouldn't store pointers to Java 
objects in C++ objects allocated with C++ new/malloc/etc, as the 
reference won't be seen by the GC and could result in premature 
collection. Pointers to Java objects should only be stored on the stack 
or in other Java objects.

regards

Bryce.




More information about the Java mailing list