posted 12 years ago
[edit]
The Wrapper class below is *broken*. Do not use it. The class loader it uses will not be able to locate classes referenced by the wrapper class.
Instead, it's better to create a custom class loader at start up, that takes the responsibility of finding all the classes the program references, and keeping the two conflicting classes separateed.
[/edit]
Let's say we have two different classes with the same name, in two different Jars:
The first one is located in Lib1.jar, the second one is located in Lib2.jar. Both are in the classpath.
In my case, I can't use the second class, because the first one is being loaded by the class loader:Invoking this program will give me a NoSuchMethodException, if I get it to compile in the first place. So if I want to use the second class, I have to write a wrapper class for it:
This wrapper provides exactly the same methods as the class I want to use, and it provides a factory class that can provide instances of the wrapper class. Using this class works as follows: