Unload Java JNI DLL

The problem

Once a dll is loaded in a java program using either System.load() or System.loadLibrary(), how do we unload it? Theoretically the dll should be unloaded when the corresponding class loader is being garbage collected.

When a class loader is garbage collected?

A class loader is garbage collected when:

  • All objects of classes loaded by the class loader should be garbage collected.
  • All references pointing to the class loader object should be null.

What class loader should we use?

It is not possible to use the default class loader because it won't be garbage collected. So, we should create our own class loader and use it instead of the default one.

Check these links for custom java class loaders implementations:

Simple Java Custom Class Loader Implementation
Java Custom URL Class Loader Implementation With Repository
Java Custom URL Class Loader Implementation With Resource List

DLL unloading example using the custom class loader

Create a file A.java and add the following code.

package com.codethesis.example;

public class A {
    public A() {}

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

    public native void print();

    public void finalize() {
        System.out.println("A garbage collected");
    }
}

You'll need to have a JNI dll mydll.dll in the classpath which contains a method print() as shown in the code. Then create a file Test.java and add this code:

package com.codethesis.example;

import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        CustomClassLoader cl = new CustomClassLoader();
        Class ca = cl.findClass("com.codethesis.example.A");
        Object a = ca.newInstance();
        Method p = ca.getMethod("print");
        p.invoke(a);
        p = null;
        ca = null;
        a = null;
        cl = null;
        System.gc();
    }
}

After System.gc() is called the custom class loader will be garbage collected and the dll wlll be unloaded.

Why do we use java reflection?

Isn't it possile just to cast the "a" object to type "A" as it is in the code below?

package com.codethesis.example;

import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        CustomClassLoader cl = new CustomClassLoader();
        Class ca = cl.findClass("com.codethesis.example.A");
        A a = (A)ca.newInstance(); //a ClassCastException is thrown!
        a.print();
        ca = null;
        a = null;
        cl = null;
        System.gc();
    }
}

Actually, it is not possible because A class is loaded by the default class loader and therefore these are two different classes. However there is a simple solution for this problem using interfaces. Create a file IA.java and add the following code inside:

package com.codethesis.example;

public interface IA {
    public void print();
}

Then modify the A class to implement this interface:

package com.codethesis.example;

public class A implements IA {
    public A() {}

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

    public native void print();

    public void finalize() {
        System.out.println("A garbage collected");
    }
}

The Test class should look like this:

package com.codethesis.example;

public class Test {
    public static void main(String[] args) throws Exception {
        CustomClassLoader cl = new CustomClassLoader();
        Class ca = cl.findClass("com.codethesis.example.A");
        IA ia = (IA)ca.newInstance();
        ia.print();
        ca = null;
        ia = null;
        cl = null;
        System.gc();
    }
}

Recommend

Pin in button

Post a new comment





You may use the following tags in the comment body: [br] for forcing new line, [code][/code] to enter programming code!

Enter the words you see *

Comments

Vishnu
2014-04-02 15:06:02
This example is good for standalone. In web application I am setting the classloader and all classes to null before running System.gc(). what should be the path of Java class in web application if the native method is called in a class that is called by JSP. Class ca = cl.findClass("com.codethesis.example.A");
J Creater
2013-07-25 08:31:42
Thank you! It worked for me!

Latest Blog Posts

A list of the top ten most popular programming languages in 2013.

A list of probably the best ten movies for geeks of all time.

Fix for this comman problem "'javac' is not recognized as an internal or external command, operable program or batch file."

Article explaining how to unload jni dll loaded by a java application.

Most Viewed Blog Posts

Article explaining how to unload jni dll loaded by a java application.

Fix for this comman problem "'javac' is not recognized as an internal or external command, operable program or batch file."

A list of the top ten most popular programming languages in 2013.

A list of probably the best ten movies for geeks of all time.

Blog Categories

Powered by LeosPHP! All rights reserved!