Continuation of the previous post.
Some workarounds to overcome the problem are:
Some workarounds to overcome the problem are:
1) Load the dll in system classloader that is available to all child
classloaders
2) Always load the dll with a different name - Bad approach3) Unload DLL by cleaning up ClassLoader cache. I tried this approach after some analysis as option1 was not feasible in my case.
Whenever a native library is loaded in a classloader, the following private static fields get updated - loadedLibraryNames and nativeLibraries.
Refer java.util.ClassLoader javadocs for more info on these fileds.
java.util.ClassLoader creates a ClassLoader$NativeLibrary(local inner class) class object
for each and every native library it loads and also maintains a cache containing the names of the DLL files that are loaded.
I have used reflection to fetch the fields - loadedLibraryNames and nativeLibraries - from ClassLoader and cleared all the entries corresponding to the DLL that i wanted to unload.
This solution worked in my case and I need not wait till the ClassLoader is garbage collected inorder to call a native method from different classloaders.
The complete source code can be downloaded from
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BxW5CDXA1SsmNGY3NWE5MmItZDJkMC00MTVlLTg0ZjQtZmI3ODIzY2NmZjQ3&hl=en_US