Re: Invoking JNI function using gcj

Andrew Haley <[email protected]>
Newsgroups gmane.comp.gcc.java.devel
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.