Tuesday, January 12, 2010

How to Write a Custom Class Loader to Load Classes from a Jar

A custom class loader is needed when the developer needs to load classes from some custom repositories, to implement hot deployment features and to allow unloading of classes.

According to Java2 class loading system, a custom class loader should subclass java.lang.ClassLoader and overrride findClass() method which is responsible for loading the class bytes and returning a defined class.

Following code is a custom class loader which loads classes from jars, inside the directory called "jar".

public class JarClassLoader extends ClassLoader {
    private String jarFile = "jar/test.jar"; //Path to the jar file
    private Hashtable classes = new Hashtable(); //used to cache already defined classes

    public JarClassLoader() {
        super(JarClassLoader.class.getClassLoader()); //calls the parent class loader's constructor
    }

    public Class loadClass(String className) throws ClassNotFoundException {
        return findClass(className);
    }

    public Class findClass(String className) {
        byte classByte[];
        Class result = null;

        result = (Class) classes.get(className); //checks in cached classes
        if (result != null) {
            return result;
        }

        try {
            return findSystemClass(className);
        } catch (Exception e) {
        }

        try {
            JarFile jar = new JarFile(jarFile);
            JarEntry entry = jar.getJarEntry(className + ".class");
            InputStream is = jar.getInputStream(entry);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            int nextValue = is.read();
            while (-1 != nextValue) {
                byteStream.write(nextValue);
                nextValue = is.read();
            }

            classByte = byteStream.toByteArray();
            result = defineClass(className, classByte, 0, classByte.length, null);
            classes.put(className, result);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

}

This code can be improved to
* Load classes from any jar inside the directory
* Dynamically pick up classes if the jar is updated (by keeping a modified time field)

33 comments:

  1. Hi Kalani, make sure your implementation doesn't break resource loading. I think I came across this issue when I wrote a JarClassLoader for one of my projects. As I remember, if you have stuff like Class.getResource(String) which tries to load jar - local resource, they won't work out of the box with this JarClassLoader.

    ReplyDelete
  2. Hi Asiri, Thanks for pointing out. Yea this class loader doesn't override ClassLoader's findResource() method. In my case I was just testing to achieve dynamism only in class loading. I guess this kinda thing will resolve that.

    public URL findResource(String name) {
    try {
    File file = new File(jarFile);
    String url = file.toURL().toString();
    return new URL("jar:"+url+"!/"+name);
    } catch (Exception e) {
    return null;
    }

    }

    Just a quick test. There may be more prof ways.

    ReplyDelete
  3. Hi Kalani, Nice post. Simple and sweet.

    ReplyDelete
  4. Hello,

    I'm developing a library that tries to imitate the Java Web Start functionality, and for doing that I am creating a custom class loader.

    I used your code as starting point and now my class loader works perfect.

    Thanks a lot!

    Greetings from Spain.

    ReplyDelete
  5. Nice to hear that. Thanks.

    ReplyDelete
  6. Hi Kalani,

    It is really a good article for a custom class loader. Your expanation is very good and very precise.Thanks for posting a good article on net.

    Thanks
    Chhote

    ReplyDelete
  7. Hi Kalani,

    A very good piece of work. The sample code works great. I guess you should also cover about your finding on the classloader aspect of Java.

    Keep up the good work and keep it posting too :-)

    ReplyDelete
  8. Thanks Manoj and Cchote...sure, I'll be posting:)

    ReplyDelete
  9. Helllo Kalani,

    Doesn't URLClassLoader already does this with improvments you have suggested...

    ReplyDelete
  10. Nice tutorial on the image splitting and concatenation.

    ReplyDelete
  11. the site provides the solutions specifically targeted towards business directories. and conjointly provide free business listings and free business advertising to all or any small business homeowners.
    Custom Directory

    ReplyDelete
  12. nice post, just stumble across your blog from stackoverflow ref..

    ReplyDelete
  13. kalani ji,,,i am in btech and my project is how to design class loader in java please help me .....my email id kumarsumit912@gmail.com...
    if their is any code related to this please send me and also how to run.

    ReplyDelete
  14. Hi kalani,
    The post is really very good.
    I have very less knowledge on class loader, so please correct me if am wrong. I understand the reason why you have overridden findClass(As your intend is to load the class from jar). But why is that you have overridden loadClass in your classloader?
    Isn't the base class(ClassLoader) does the similar implementation? if we use the default constructor to create classloader the parent loader would be systemclassloader. So you reduce rewriting this logic in your implementation of findClass.

    PS :
    I am not commenting on your code, it's just that I am trying to understand better. Please do provide your thoughts :)

    ReplyDelete
  15. Hi Kalani,

    Its a great post to see how a class loader works internally, especially how the internals of a jar file can be read and how defineClass() method could be used.

    But isn't it possible to achieve the same thing in your code by doing something like;

    java.io.File jarFile = new java.io.File("path_to_your_jar_file");
    java.net.URL fileURL = jarFile.toURL();
    URLClassLoader clsLoader = new URLClassLoader(new URL[] {fileURL});

    Or am I missing something important here?


    ReplyDelete
  16. This post helped me at need of the hour.

    Thanks a ton

    ReplyDelete
  17. Custom writings should cover the entire coursework activities and this should be stated by the professor before he or she delivers the custom writing to the students for undertakings this site will help you so.

    ReplyDelete
  18. http://www.bestcustomessay.org/essays will not provide your personal details to other people at any circumstance.

    ReplyDelete
  19. Hi Kalani,
    How to take care of laoding multiple jars by the custom classaloder

    Pass it as URL[] ?

    Regards
    Manoj

    ReplyDelete
  20. Good instruction, this is really a big help to all computer geeks out there. Hope you can write more mightystudents essay like this one.

    ReplyDelete
  21. I used your code as starting point and now my class loader works perfect.
    Gangway

    ReplyDelete
  22. First of all I would like to thank for this nice articles. It really helped me a lot writing a custom loader.

    Still I have some questions on this. With out our custom loader can't we load the classes from out custom jar?

    Little confused on this.

    Can you please share your answers on my mail id pretheshkumarbhalotia@gmail.com

    Thanaks a ton Kalani.

    ReplyDelete
  23. Anonymous10:21 PM

    Hi Kalani,

    I get the following error while using this code

    java.lang.NullPointerException
    at java.util.zip.ZipFile.getInputStream(Unknown Source)
    at java.util.jar.JarFile.getInputStream(Unknown Source)


    Can you help?

    ReplyDelete
  24. Hi Kalani,
    You don't want parent classloader to load class.Why did you invoke super method?

    ReplyDelete

  25. It is good to note that only a few of the programmers are usually prepared to cope with Java programming. This is due to the complexity which is attached to the implementation and development of Java programs. Through such forums, such complexity is usually minimized and it gives a developer an easy time. You can request for computer science projects Literature Review Paraphrasing Assistance online.

    ReplyDelete
  26. Good information to be known and to be discussed.Good to learn something new.and its nice to see all discussing about the rectifications.good share of knowledge.
    lenovo service centre chennai
    lenovo service center
    lenovo mobile service center near me
    lenovo mobile service centre in chennai

    ReplyDelete
  27. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.

    sap crm training in bangalore

    sap ehs training in bangalore

    sap bw training in bangalore

    sap hana training in bangalore

    sap hr training in bangalore

    sap mm training in bangalore

    sap pm training in bangalore

    sap pp training in bangalore

    ReplyDelete
  28. Hi, your article was of great help. I loved the way you shared the information, thanks.

    Sap EHS training in bangalore

    ReplyDelete
  29. Anonymous11:56 AM

    Autocad Training in Bangalore
    Autocad classes in Bangalore

    ReplyDelete
  30. Useful information. Fortunate me I found your website accidentally, and I’m stunned
    why this twist of fate did not took place in advance.
    I bookmarked it.
    cheq out my website Satta King Fast
    I do not know if it’s just me or if perhaps everyone else experiencing issues with your site.
    It appears as if some of the text in your content are
    running off the screen. Can someone else please provide feedback
    and let me know if this is happening to them too? This might be a problem with my web browser because
    I’ve had this happen before. thanks.
    Our Site Black Satta King 786 And Charminar Satta King
    Satta Matka | Madhur Matka

    ReplyDelete
  31. Useful and informative information. You can also get reliable dissertation and other writing help services from our leading assignment help writing company (https://sureassignmenthelp.com/)at an affordable price.

    ReplyDelete
  32. replica bags sydney gucci fake v5d93v3o68 replica bags wholesale in divisoria replica bags bangkok Going Here j4l57t6l76 replica nappy bags click here to read r9l91r6l63 high quality replica bags replica bags and shoes u2r92m9m31

    ReplyDelete