Hi, I want to build an application for administration of a number of content management servers running on separate physical machines (with different OS). Each CM-server exposes itself via a vendor-supplied server API (Java based). That API may be called by using a vendor-supplied client side API (also Java based). In order to manage a specific server I need to use the correct version of the client-side API. Say for example that I want to manage two CM-Servers that uses API version 4.1 and API version 3.2 respectively. It is not possible for me to use the 4.1 API to call the 3.2 API. I need to load the correct client API classes dynamically. Does anyone have any idea how to solve this problem?
Use a different ClassLoader for each version of the code. eg your application has its default ClasslOader, and there are 2 jars V1.jar and V2.jar that are not on the application CLASSPATH! Inside your application, have two URLClassLoaders, one that refers to V1.jar and a second that refers to V2.jar It is possible to load Classes and instantiate them as Objects. You can't cast them since you don't have them on the system classpath to compile, but you can use reflection to execute the methods you want. Rather than excessive use of reflection, you may want to create a wrapper that delegates to the particular version you are currently refering to. Hope this helps, Dave.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Thanks a lot for the answer! Another question - The reason for not putting the two jar files in your example on the CLASSPATH, is it because you want to avoid it being loaded by a parent classloader? Per
That's right. The default behaviour for ClassLoaders is to try to let the parent load it first. Therefore if your URLClassLoaders try to load their own version, they'll defer to the parent and it'll load the first version it sees and that's all you'll get. Dave