I'd like to implement my own custom ClassLoader. Basically it should do
exactly what the default OSGi ClassLoader does.

I am using Karaf/Felix, so in my case that's
org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader

The reason for implementing my own class loader is this: I use JNI to load
a DLL (with System.load()). But everytime I redeploy my bundle I get a
UnsatisfiedLinkError exception: Native Library xyz.dll already loaded in
another classloader.

This is, because the DLL is already loaded by the JVM - and stays as long
as the ClassLoader that loaded it is NOT garbage collected.

So I want to do something like this:

    CustomClassLoader cl = new CustomClassLoader();
    Class ca = cl.findClass("myPackage.MyClass");
    Object a = ca.newInstance();
    Method p = ca.getMethod("myMethod");
    p.invoke(a);
    p = null;
    ca = null;
    a = null;
    cl = null;
    System.gc();

Hopefully after that the CustomClassLoader is garbage collected - and the
DLL is unloaded from the container.

*What I found strange:* Even so each bundle has it's own ClassLoader, an
"osgi:uninstall <bundle>" does not unload the DLL - which means that the
bundles ClassLoader is still alive (and not garbage collected).

Reply via email to