Invoking JNI function using gcj
Andrew Haley
aph@redhat.com
Wed May 27 08:48:00 GMT 2009
Vaijayanthi Mala Suresh wrote:
> I have written an HelloWorld.java as given below
>
> public class HelloWorld {
> public native void natPrint();
>
> public HelloWorld()
> {
> System.out.println("Hello from constructor");
> }
> public void printline()
> {
> System.out.println("Hello from printline");
> natPrint()
> }
> public static void main(String [] args) {
> System.out.println("Hello from main");
> HelloWorld ht = new HelloWorld();
> ht.printline();
> }
> static
> {
> System.loadLibrary("HelloWorld");
> }
> }
>
> I have compiled it with gcj as given below
> gcj -c -fjni HelloWorld.java
> gcj -C HelloWorld.java
> gcjh HelloWorld
> gcjh -jni HelloWorld -o HelloNative.h
> gcjh -stubs -jni HelloWorld
> g++ -I. -c HelloWorld.c natHelloWorld.o
> gcj -shared natHelloWorld.o -o libHelloWorld.so
You can't mix JNI and CNI like this. Don't use the -jni commands.
> Copied the .so file into the folder as /lib and it set in LD_LIBRARY_PATH.
>
> I have a cppstartup.cpp which does the JVM Initialization and has
> invokes HelloWorld as given below
>
> #include "HelloWorld.h"
>
> using namespace java::lang;
>
> try
> {
> JvCreateJavaVM(NULL);
> JvAttachCurrentThread(NULL, NULL);
>
> String *message = JvNewStringLatin1("Hello from C++");
> JvInitClass(&System::class$);
> System::out->println(message);
>
> HelloWorld *ht = new HelloWorld();
> ht->printline();
>
> JvDetachCurrentThread();
> }
> catch (Throwable *t)
> {
> System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
> t->printStackTrace();
> }
>
> This throws an exception me "Unsatisfied Linker Error"
>
> Can someone help me in this regard?
It doesn't even compile for me. Your cppstartup.cpp is incomplete.
Andrew.
More information about the Java
mailing list