Re: Interfacing static C libraries with JNI

From: Gordon Beaton (not_at_for.email)
Date: 03/10/04


Date: 10 Mar 2004 17:01:04 +0100


On 10 Mar 2004 06:42:52 -0800, Atulvid wrote:
> class Prototype{
> public native void abc(int flag, String message, String command);
> static{
> System.loadLibrary("libwrapper.so");

The above would have caused an UnsatisfiedLinkError, not the error
you've posted below. Is this code really cut and pasted from a real
example?

You probably meant to write

  System.loadLibrary("wrapper");
or
  System.load("/some/path/libwrapper.so");

> gcc -fPIC -shared -I$JAVA_HOME/include -I$JAVA_HOME/include/solaris
> Prototype.c -L$HOME/jni -lxyz -o libwrapper.so

Add -D_REENTRANT to the compile flags.

> ld.so.1: /usr/java/bin/sparc/native_threads/java:fatal: relocation
> error: file /home/jni/libwrapper.so: symbol abc: referenced symbol
> not found
> Killed
>
> I tried listing symbols of libwrapper.so using
> nm -aC libwrapper.so | grep abc
> 0000058c T Java_Prototype_call_abc
> U abc

Here, nm confirms that abc is *undefined* (U) in your shared library.

- Are you sure that abc is a public symbol in libxyz.a? Again, use nm.

- Do the symbols look different when you run nm without -C? In
  particular, do "nm libxyz.so" and "nm libwrapper.so" (without -C)
  show the *same* abc?

- Are you linking against the libxyz.a you think you are? Use -v when
  you compile.

- Use "ldd libwrapper.so" to check for other runtime dependencies.

- Make sure you don't have more than one libwrapper.so lying around
  (since I'm guessing that isn't the real name you're using). On
  Solaris, use crle to get the list of directories that are searched.

> I do not understand why my Java code is unable to find the abc()
> method. DO I need to include header file for abc() as well in
> Prototype.c ?

If abc is a public symbol in libxyz.a then it is most likely declared
in a header file like xyz.h or somesuch and you should include that
header in any source code that refers to the symbol.

/gordon

-- 
[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e


Relevant Pages