tags:

views:

1128

answers:

2

Hey Folks,

In a Java program i am writing i make a jni call to a dll and load the library on startup as follows

static
{
   System.loadLibrary("LdapAuthenticator2");
}

I then implemented another class that loads the same library and am getting an error saying that the library is already loaded, is there any way to check if the library is already running?

Thanks,
-Pete

+1  A: 

What kind of an error? If it's an exception, can you just catch it?

Another approach would be to make exactly one class responsible for loading the library. You could make loading the library part of the class's static initializer, and then loading the class == loading the library.

EDIT: the javadocs for Runtime.loadLibrary() (which System.loadLibrary calls) even suggests the static initializer approach:

If native methods are to be used in the implementation of a class, a standard strategy is to put the native code in a library file (call it LibFile) and then to put a static initializer:

     static { System.loadLibrary("LibFile"); }

within the class declaration. When the class is loaded and initialized, the necessary native code implementation for the native methods will then be loaded as well.

The javadocs also say:

If this method is called more than once with the same library name, the second and subsequent calls are ignored.

which makes me even more curious about the error you're getting.

Laurence Gonsalves
I guess he gets a UnsatisfiedLinkError because two different classloaders try to load the same native library
jitter
Some relevant links to the problem http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4750956 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5075039
jitter
yeah jitter is right, that is the error i am getting, thanks for the links jitter
Petey B
+3  A: 

Check my answer to this question

How do I get a list of JNI libraries which are loaded?

The solution works, unfortunately the poster of the question seems to have problems with a non SUN compatible JVM or a too restrictive SecurityManager.

Link to the sample POC source code.

List loaded JNI libraries java sourcecode

jitter