Link java object files into cpp app
Bryce McKinlay
bryce@waitaki.otago.ac.nz
Sun Feb 17 00:27:00 GMT 2002
Lars Andersen wrote:
> I have compiled some java-files into object-files using gcj.
> I have also generated headerfiles for each of my java-classes using gcjh.
>> Now i want to call a static java method from a c++ source, and link it
> all together.
>> Can CNI or JNI be used for this purpose?
Yes. The CVS version of GCJ supports invocation APIs for both CNI and
JNI code which allow you to call into Java classes from a C++ program.
For CNI you must call JvCreateJavaVM once before making any other Java
calls, and JvAttachCurrentThread once for each native thread which was
not created by the regular java threads interface.
Example:
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
int main(int argc, char *argv)
{
using namespace java::lang;
JvCreateJavaVM (NULL);
JvAttachCurrentThread (NULL, NULL);
System::out->println(JvNewStringLatin1 ("Hello from C++"));
}
$ c++ InvokeCNI.cc -lgcj
$ ./a.out
Hello from C++
JNI invocation is also supported, and should work more or less the same
as it does in other Java implementations.
regards
Bryce.
More information about the Java
mailing list