It is possible and actually fully supported and works brilliantly.
I had to do this in a production environment, and in the sun the JVM is a solid.
Basically, if you load a library from another classloader, then it will load another copy of the library. It is so simple.
I would not recommend doing this unless you really need to ... but it works.
Alternatively, depending on your specific requirements, you can simply exclude it from the process and have a simple protocol (using say jetty / xstream / httpclient or netty) between the client and different servers, each of which has a different version of the dll.
This is essentially due to the fact that you are writing a class loader
public class MyClassLoader extends URLClassLoader { protected String findLibrary(String libName) { if ( libName.equals("mylib.dll")) { return "full/path/to/library"; } else { super.findLibrary(libName); } } }
Then you organize the loading of your class implementation using the appropriate class loader ...
public interface Implementation { } public class ImplementationLookerUpper { Classloader v1 = new MyClassloader(version1); Classloader v2 = new MyClassloader(version2); public Implementation implementationUsingVersion(Version someversion) { Classloader classloader = pickCorrectClassLoaderForVersion(someVersion); return (Implementation) classloader.loadClass(RealImplementation.class.getName()).newInstance(); } }
That kind of thing .....
time4tea
source share