简体   繁体   中英

loading of two different versions of jars at runtime through java

I am working on a project in which for few functionalities POI 2.5 version jars is being used and i have develop a new functionality for which i require POI version 3.10 jars so now the issue is that previous functionality is not working properly as it is working perfectly on poi 2.5 version jar

now if i keep both the versions of poi jars in my class path is there any way by which only for my classes i can call poi 3.10 version explicitly and for the previous functionality poi 2.5 version is being called

by default if i keep both the versions of poi jars then always the call goes in poi 2.5 versions of jar so i want for my class say class abc explicitly poi 3.10 version is being called

Please advise can i use class loaders explicitly here and also please advise is it possible if i can create the object of class dynamically also

class ABC 
{

//version of poi 3.10 being called here 
HSSFWorkbook workbookXls = new HSSFWorkbook();

}

class def which is being using earlier version of POI 2.5

class DEF
{

//version of poi 2.5 being called here 
HSSFWorkbook workbookXls = new HSSFWorkbook();

}

Yes this can be done, loading each jar from a different classloader.

  1. Move both jars out from your current classpath
  2. Create a URLClassLoader for each of them jar, with your actual classloader as parent
  3. Use reflection to deal with POI

You can also refactor one the jars to change its packages and avoid collisions. This can be done automatically if your are using maven with the jar-jar MOJO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM