wood burning stoves 2.0*
The moose likes Java in General and the fly likes Using different versions of the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using different versions of the same class" Watch "Using different versions of the same class" New topic
Author

Using different versions of the same class

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using different versions of the same class
 
Similar Threads
Am I missing something?
Best Container
Load Balancing - Handling Session between multiple servers.
please answer this 128 questions for WLS. Urgently!
How to implement certificate/security into web service client