This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Link java object files into cpp app
- From: Bryce McKinlay <bryce at waitaki dot otago dot ac dot nz>
- To: Lars Andersen <lars at jatak dot com>
- Cc: java at gcc dot gnu dot org
- Date: Sun, 17 Feb 2002 10:00:07 +1300
- Subject: Re: Link java object files into cpp app
- References: <3C6E7F3B.1030600@jatak.com>
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.