Java, Classpath, Classloading => Multiple Versions of the same jar/project

前端 未结 5 2065
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 12:13

I know this may be a silly question for experienced coders. But I have a library (an http client) that some of the other frameworks/jars used in my project require. But all

5条回答
  •  无人共我
    2020-11-22 12:51

    You can use the URLClassLoader for require to load the classes from a diff-2 version of jars:

    URLClassLoader loader1 = new URLClassLoader(new URL[] {new File("httpclient-v1.jar").toURL()}, Thread.currentThread().getContextClassLoader());
    URLClassLoader loader2 = new URLClassLoader(new URL[] {new File("httpclient-v2.jar").toURL()}, Thread.currentThread().getContextClassLoader());
    
    Class c1 = loader1.loadClass("com.abc.Hello");
    
    Class c2 = loader2.loadClass("com.abc.Hello");
    
    BaseInterface i1 = (BaseInterface) c1.newInstance();
    
    BaseInterface i2 = (BaseInterface) c2.newInstance();
    

提交回复
热议问题