0

In my project class path poi 2.5 version is already there and on runtime i want to load poi 3.5 jar as for two java classes in my project require poi 3.5 version , so for that I have written class loader that will call the poi 3.5 jar on runtime please advise what is wrong now as it is not loading poi 3.5 version jar at runtime

below is my classloader

public class MyClassLoader {

    private static final Class[] parameters = new Class[] {URL.class};

    public static void addFile(String s) throws IOException
    {
        File f = new File(s);
        addFile(f);
    }

    public static void addFile(File f) throws IOException
    {
        addURL(f.toURI().toURL());
    }

    public static void addURL(URL u) throws IOException
    {
        URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class sysclass = URLClassLoader.class;

        try {
            Method method = sysclass.getDeclaredMethod("addURL", parameters);
            method.setAccessible(true);
            method.invoke(sysloader, new Object[] {u});
        } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
        }

    }

}

and here i am calling from inside main method one of the method of poi 3.5 class which is newly added in 3.5 but it is not loaded

 MyClassLoader.addFile("C:\\Release14branchupdated\\lib\\thirdparty\\POI-3.5\\poi-3.10-FINAL.jar");
       Constructor<?> cs = ClassLoader.getSystemClassLoader().loadClass("org.apache.poi.ss.formula.FormulaCellCacheEntrySet").getConstructor(String.class);
      System.out.println("$$$$$$$$$$"+cs.getName());           
       org.apache.poi.ss.formula.FormulaCellCacheEntrySet instance = (org.apache.poi.ss.formula.FormulaCellCacheEntrySet)cs.newInstance();
       instance.iterator();

now upon execution the error that i am getting is not ble to find class FormulaCellCacheEntrySet

sss
  • 161
  • 1
  • 1
  • 13
  • you are loading the jar in your `MyClassLoader` and then you are trying to get an instance from the `ClassLoader.getSystemClassLoader()`? – AntJavaDev Mar 10 '16 at 19:29
  • So please advise how to proceed as I want instance of that class that us poi 3. 10 final jarone – sss Mar 10 '16 at 19:30
  • and you say that poi 2.5 is already loaded and in use , check this answer [here](http://stackoverflow.com/questions/11759414/java-how-to-load-different-versions-of-the-same-class) – AntJavaDev Mar 10 '16 at 19:32
  • Yeah basically my project contain 10 legacy classes which use poi, 2.5 jar and I have written new classes which use poi 3.10 so I just only for my classes I want to use poi 3.10 only – sss Mar 10 '16 at 19:34
  • Thanks for sharing the link request you to please advise how can i similarly have two class loaders and loading them separately and having instance of each one that will help to grasp more – sss Mar 10 '16 at 20:00
  • well if you check what they are proposing below in the answer , it is not the best way of an application to be operating as you have by yourself to manage the class loading / unloading which will not be so easy and surely will lead to OOM exceptions. also why you need the same class from an older version ? i suppose youll have to change a bunch of code ? but still this is called project refactoring , in case that your dependencies are getting old – AntJavaDev Mar 10 '16 at 20:04
  • Well agree with you but rite now lots of methods have been changed so I just want that my classes of total count 2 should only get new version of poi 3.10 only rest of the legacy code should continue to use poi, 2.5 jar – sss Mar 10 '16 at 20:08

0 Answers0