0

I have created a CustomClassLoader that inherits from the ClassLoader to override loadClass method.

Below is the new CustomClassLoaded

    public class DesignFactoryClassLoader extends ClassLoader{

        public DesignFactoryClassLoader(ClassLoader parent) {
            super(parent);
        }

        public String classCanonicalName= null;

        public Class loadClass(String canonicalName) throws ClassNotFoundException {
             if(!this.classCanonicalName.equals(canonicalName))
                    return super.loadClass(canonicalName);
            try {                   

                    URL classLoadUrl = new URL(this.reloadClassUrl);
                    URLConnection connection = classLoadUrl.openConnection();
                     InputStream input = connection.getInputStream();
                     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                     int data = input.read();
                    int data = input.read();

                    while(data != -1){
                        buffer.write(data);
                        data = input.read();
                    }
                    input.close();
                    byte[] classData = buffer.toByteArray();

                    return defineClass(canonicalName,
                            classData, 0, classData.length);
                } catch (Exception e) {
                    //TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return null;
          }
}

To reload a particular class from the disk this is how I do it

ClassLoader parentClassLoader = DesignFactory.class.getClassLoader();
DesignFactoryClassLoader designFactoryClassLoader = new DesignFactoryClassLoader(parentClassLoader);
designFactoryClassLoader.classCanonicalName = classCanonicalName;
Class reloadedCls = designFactoryClassLoader.loadClass(classCanonicalName);

The problem that I now have is: When I needed to create an instance of MyClass after the MyClass.class has been modified I created it using the CustomClassLoader and the .class file has been loaded into the customClassLoader. The second time I need to create an instance I will reload the .class file again from the disk with my logic. How can I avoid this?

  • What exactly are you trying to solve? This could be a bad idea since reloading a changed class could mean that some other class using this class's APIs might fail at run time since, say, the method signature changed. – Pavan Feb 04 '15 at 21:56
  • I solved the problem by loading the class files and holding them in an array list. The problem we is that we have a about 500+ calculator classes that are loaded into a Design engine when the app starts. When we find an issue with one of the calculators we redeploy all the calculators and restart the Design engine, this is causing a down time for the service since restart takes sometime. – Kishore Vanapalli Aug 14 '15 at 03:32
  • I am trying to ONLY redeploy the one .class calculator that has the issue and reload that calculator into the Engine without having to restart the whole app. When the new class files are deployed we call a backend API method to clear the Array List and the next time the engine is looking for the class file it cant find it in the array list and it will reload it from the disk. – Kishore Vanapalli Aug 14 '15 at 03:38

0 Answers0