* Invoking JNI function using gcj
@ 2009-05-27 8:34 Vaijayanthi Mala Suresh
2009-05-27 8:48 ` Andrew Haley
2009-05-27 9:48 ` Bryce McKinlay
0 siblings, 2 replies; 3+ messages in thread
From: Vaijayanthi Mala Suresh @ 2009-05-27 8:34 UTC (permalink / raw)
To: java
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
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?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Invoking JNI function using gcj
2009-05-27 8:34 Invoking JNI function using gcj Vaijayanthi Mala Suresh
@ 2009-05-27 8:48 ` Andrew Haley
2009-05-27 9:48 ` Bryce McKinlay
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Haley @ 2009-05-27 8:48 UTC (permalink / raw)
To: Vaijayanthi Mala Suresh; +Cc: java
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Invoking JNI function using gcj
2009-05-27 8:34 Invoking JNI function using gcj Vaijayanthi Mala Suresh
2009-05-27 8:48 ` Andrew Haley
@ 2009-05-27 9:48 ` Bryce McKinlay
1 sibling, 0 replies; 3+ messages in thread
From: Bryce McKinlay @ 2009-05-27 9:48 UTC (permalink / raw)
To: Vaijayanthi Mala Suresh; +Cc: java
On Wed, May 27, 2009 at 9:34 AM, Vaijayanthi Mala Suresh
<vaijayanthimalas@gmail.com> wrote:
> 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
>
> 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);
It looks like you're confusing JNI with CNI. You mentioned JNI and
used the "-jni" option to gcjh, but the C++ code you provided appears
to be CNI.
Bryce
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-05-27 9:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-27 8:34 Invoking JNI function using gcj Vaijayanthi Mala Suresh
2009-05-27 8:48 ` Andrew Haley
2009-05-27 9:48 ` Bryce McKinlay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).