JNI newbie cries for help

D

Dominik Kaspar

i have seen a few older posts about the same or similar questions. but
they were either misunderstood, badly answered or i'm too dumb to
understand them. so i try to ask it again:

i have studied the online tutorial on
http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/index.html
and tried out the code. in step 6 the java.lang.UnsatisfiedLinkError
is mentioned and it happended as well in my program:

java.lang.UnsatisfiedLinkError: no hello in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1403)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:832)
at jni_test.HelloWorld.<clinit>(HelloWorld.java:16)
Exception in thread "main"

But this is NOT my problem. I was able to fix that. Now the native
code seems to be found but there's this other error appearing:

java.lang.UnsatisfiedLinkError: displayHelloWorld
at jni_test.HelloWorld.displayHelloWorld(Native Method)
at jni_test.HelloWorld.main(HelloWorld.java:20)
Exception in thread "main"

Can anybody please help me explain this behaviour?
What did I do wrong?!

Thanks a lot in advance
Dominik
 
G

Gordon Beaton

But this is NOT my problem. I was able to fix that. Now the native
code seems to be found but there's this other error appearing:

java.lang.UnsatisfiedLinkError: displayHelloWorld
at jni_test.HelloWorld.displayHelloWorld(Native Method)
at jni_test.HelloWorld.main(HelloWorld.java:20)
Exception in thread "main"

Can anybody please help me explain this behaviour?
What did I do wrong?!

You are trying to invoke a method "displayHelloWorld" but the correct
method wasn't found in the native library. That means that the JVM
couldn't find a method in the library that has the *exact* name and
signature that javah generated in the header file.

- If there is a package declaration in the java source, note that you
need to provide the fully qualified classname when you run javah or
it won't generate the correct names (you should recognize the
package name as components in the generated symbols).

- If you are using a C++ (not C) compiler, you need to make sure that
your native functions are all declared extern "C". If you followed
the exact names and signatures that javah suggested *and* you
included the header file, your functions are already declared extern
"C". If you didn't, why not?

- If you are linking with gcc on windows it may have changed your
symbol names subtly, adding characters like "@8" that you didn't ask
for. If that's the case, you need to tell it not to do that. Add the
following switch to the link command: -Wl,--kill-at

- Use a system specific tool to examine the actual generated library
file. For example nm on unix or linux, and dumpbin or quickview on
windows.

If that doesn't help you solve it, post the source of your C and Java
files and the header file that was generated by javah, and say what
compiler and operating system you are using.

/gordon
 
D

Dominik Kaspar

- If there is a package declaration in the java source, note that you
need to provide the fully qualified classname when you run javah or
it won't generate the correct names (you should recognize the
package name as components in the generated symbols).

Hey thanks. That solved it! My problem was that I took the exact same
class from the tutorial, but my JBuilder program just made a new
package that I didn't notice. So when I removed the package line,
everything worked!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top