Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / February 2005

Tip: Looking for answers? Try searching our database.

JNI System.loadLibrary problem (shared library has global variable)

Thread view: 
Valentine - 21 Feb 2005 20:20 GMT
I've created a C shared library (libSysEventProxy.so) that contains a
JNI wrapper that calls functions in another shared library
(libnetcomm.so); this shared library in turn calls functions in yet
another library (libnetgen.so). Thus, the static initializer in my Java
class loads the libraries in order:

static {
   System.loadLibrary( "netgen" );
   System.loadLibrary( "netcomm" );
   System.loadLibrary( "SysEventProxy" );
}

All the shared libraries reside in the same directory (which is listed
in my LD_LIBRARY_PATH environment variable), and the ClassLoader
locates the libraries OK.
The first shared library loaded (libnetgen.so) contains a global
variable defined and initialized as:

int someOldFlag = -1;

I used the nm utility to verify that someOldFlag is indeed listed in
the initialized data section of the libnetgen.so symbol table. This
library loads OK.
The second shared library (libnetcomm.so) declares a reference to that
variable as:

extern int someOldFlag;

The ClassLoader locates the library OK and tries to load it, but it
then throws the aforementioned UnsatisfiedLinkError, with the
traceback:

Exception in thread "main" java.lang.UnsatisfiedLinkError:
/spec_platform/netcomm/libnetcomm.so.1:
/spec_platform/netcomm/libnetcomm.so.1: undefined symbol: someOldFlag
       at java.lang.ClassLoader$NativeLibrary.load(Native Method)
       at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1586)
       at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1511)
       at java.lang.Runtime.loadLibrary0(Runtime.java:788)
       at java.lang.System.loadLibrary(System.java:834)
       at gbl_fw.vdasif.SysEventProxy.<clinit>(SysEventProxy.java:31)
       at SysEventProxyTest.main(SysEventProxyTest.java:13)

Thus, it appears that the ClassLoader can't link the variable
declaration in
libnetcomm.so to its definition in libnetgen.so.

Question: What can I do to make the ClassLoader load all of the
libraries cleanly?

Thank you in advance!

p.s. gcc version: gcc (GCC) 3.3.2 20040119 Red Hat Linux (3.3.2-8)
Gordon Beaton - 21 Feb 2005 20:52 GMT
> I've created a C shared library (libSysEventProxy.so) that contains
> a JNI wrapper that calls functions in another shared library
[quoted text clipped - 7 lines]
>     System.loadLibrary( "SysEventProxy" );
> }

When you build a shared library with dependencies on another shared
library, you need to specify the dependencies at link time.

Here's one way:

 gcc -Wall $JNIFLAGS -fPIC SysEventProxy.c -o SysEventProxy.o
 gcc -shared -f libnetcomm.so SysEventProxy.o -o libSysEventProxy.so

At run time (also when using ldd, see below), the libraries
libSysEventProxy.so depends on need to be in places where the dynamic
linker can find them. That means one of /lib, /usr/lib, or a directory
mentioned in /etc/ld.so.conf, or in LD_LIBRARY_PATH.

Use "ldd libSysEventProxy.so" to see what runtime dependencies your
resulting library has, and which ones are still unresolved.

In your java application, just load libSysEventProxy.so, not the
others. Symbols loaded by one call to loadLibrary() are not visible to
code loaded in separate calls to loadLibrary().

/gordon

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.