Support for 2 versions of the API in Java

I am trying to find a way to support two different versions of the API in my code base. They have the same package names, but they work differently under the hood (two completely different systems). How can i do this?

Both of these APIs are also dependent on Bouncy Castle, but they use different versions. How do I also take this into account?

+6
source share
3 answers

The solution I'm starting with is ... Loading the API into a custom classloader that first loads the child class above the parent class. If you compile Bouncy Castle inside the API, you don’t have to worry about downloading it separately. If you dynamically load the Bouncy Castle jar at run time, then in the custom classloader you add Bouncy Castle and your API to this classloader. Using URLClassLoader and see my link below for the last parent load.

How to create the parent-last / parent class of ClassLoader in Java or How to override an old version of Xerces that has already been loaded into the parent CL?

+1
source

I would not recommend this if you do not know what you are doing, but you can use URLClassLoader as follows:

 URLClassLoader classLoaderA = URLClassLoader.newInstance(new URL[] {new URL("versionA.jar")}); URLClassLoader classLoaderB = URLClassLoader.newInstance(new URL[] {new URL("versionB.jar")}); 

Download class:

 classLoaderA.loadClass("SomeClass"); 

Another option is to look at OSGI .

+2
source

You can create a custom class loader to load the appropriate classes based on the logic needed to load one version of the API into another.

0
source

All Articles