I also tried this:
System.loadLibrary("libFoo.so"); // also tried without the .so
extension

That gave me:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libFoo.so
in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)

If I switch to using just this:
System.loadLibrary("Foo"); // no lib prefix, no .so extension

Then I again get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: init

Does anyone know what's going on here?
I've never worked with shared libraries and JNI before, so I'm not even
sure if this is how things are supposed to work. Is this how I should
be loading a shared library?

Also, if this helps (doesn't mean much to me), this is what ldd says
about my library:

Advertisements

load the library with System.loadLibrary("Foo"); and let the vm
sort out the libFoo.so.

Some things to watch for.

Undefined symbols in the library. I've noticed that JNI will fail to
load libraries with undefined symbols even if they are not referenced.
I've had libraries that could be called from C just fine but fail to load
via JNI due to some overlooked symbol. Use the nm utility to locate
these.

Dependent libraries. This is not your case since yours is statically
linked, but if libFoo.so depended on libBar.so in the same directory
putting that directory in java.library.path would not help. You need to
put the directory on the system path (LD_LIBRARY_PATH) so that ld can see
it too.

Advertisements

Timothy Bendfelt wrote:
> load the library with System.loadLibrary("Foo"); and let the vm
> sort out the libFoo.so.

I tried that, but that gives me:
Exception in thread "main" java.lang.UnsatisfiedLinkError: init
> Some things to watch for.
>
> Undefined symbols in the library. I've noticed that JNI will fail to
> load libraries with undefined symbols even if they are not referenced.
> I've had libraries that could be called from C just fine but fail to load
> via JNI due to some overlooked symbol. Use the nm utility to locate
> these.

Unfortunately, I'm really not familiar with this stuff and am not sure
what exactly this is telling me and what "undefined symbols" really
means.
The above symbols look like functions from some standard
IO/string/memory library. Maybe that library needs to be explicitly
loaded? Or maybe the system can't find that library?
> Dependent libraries. This is not your case since yours is statically
> linked, but if libFoo.so depended on libBar.so in the same directory
> putting that directory in java.library.path would not help. You need to
> put the directory on the system path (LD_LIBRARY_PATH) so that ld can see
> it too.

Thank you for this, I'll keep that in mind although, like you said,
this looks like a statically linked library.

Does this reflect the true name of the package and the class? It looks
suspiciously like you copied it from Jean-Francois' example. I.e. is
there a package declaration in the Java code, did you specify the
fully qualified classname when you ran javah, and does the symbol name
in the generated header agree with the actual name in your native
code?

What does "nm libFoo.so | grep init" tell you?

Unrelated to this problem, you must call ReleaseStringUTFChars() when
you're finished using the pointer returned by GetStringUTFChars().

Gordon Beaton wrote:
> On 24 Jul 2006 19:49:56 -0700, OtisUsenet wrote:
> > Here it is:
> >
> > JNIEXPORT jboolean JNICALL
> > Java_com_example_MyApp_init(
>
> Does this reflect the true name of the package and the class? It looks
> suspiciously like you copied it from Jean-Francois' example. I.e. is
> there a package declaration in the Java code,

Yes, that's the true name of the package and the class.
> did you specify the
> fully qualified classname when you ran javah, and does the symbol name
> in the generated header agree with the actual name in your native
> code?

I didn't build this, I'm really just trying to use it from my java app.
I know the shared library normally works, it's used in a production
system, but I don't know how exactly it is used and whether there are
some dependencies that my system just doesn't satisfy or at least it
doesn't find them.
> What does "nm libFoo.so | grep init" tell you?

$ nm libFoo.so | grep init
0000c02b T Java_com_example_MyApp_init
> Unrelated to this problem, you must call ReleaseStringUTFChars() when
> you're finished using the pointer returned by GetStringUTFChars().

On 25 Jul 2006 09:55:49 -0700, OtisUsenet wrote:
> I didn't build this, I'm really just trying to use it from my java
> app. I know the shared library normally works, it's used in a
> production system, but I don't know how exactly it is used and
> whether there are some dependencies that my system just doesn't
> satisfy or at least it doesn't find them.

If you are on an elf system (like Linux), you can use readelf -d to
see what other libraries libFoo.so depends on. Look for NEEDED
entries. On other systems you can use ldd or objdump -p or similar
tool to see the same information.

If the library was built with dependencies on other libraries, they
will either be resolved recursively when the library is loaded or the
load will fail. Those other libraries must be found by the system
dynamic linker (e.g. ld.so) which looks in some standard places, some
configurable places, and in the places mentioned in your
LD_LIBRARY_PATH (see your system documentation).

However since your UnsatisfiedLinkError occurs when you attempt to
invoke the method, that seems to indicate that the method needed by
the JVM is missing from the library. That could mean that the symbol
does not have the expected name or that its name was mangled in some
way by the compiler.

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Ask a Question