I have two modules in my application. These modules share some common JARs which contain some static HashMaps
. I want these modules to be loaded using different Class Loaders. Each Class Loader will have a custom class path that contains a set of locations.
URL [] urlsForModule1=getURLsForModule1();
URL[] urlsForModule2=getURLsForModule2();
Thread.currentThread().setContextClassLoader( new URLClassLoader( urlsForModule1, ClassLoader.getSystemClassLoader().getParent() ) );
//Perform the activities needed for Module 1
String valueForModelue1=CommonClass.getValueFromMap("someKey");
//The value is already set in some other place and this should give a specific value for Module 1
Thread.currentThread().setContextClassLoader( new URLClassLoader( urlsForModule2, ClassLoader.getSystemClassLoader().getParent() ) );
//Perform the activities needed for Module 2
String valueForModelue2=CommonClass.getValueFromMap("someKey");
//The value it should give a specific value for Module 2
But the problem that I'm facing is with the common Class files which are there in the Common JARs. Since these Class files are already loaded (before the above lines of code in some other classes) the staic HashMaps retain their previuos values. Is it possible to tell all the code below
Thread.currentThread().setContextClassLoader( new URLClassLoader( urlsForModule1, ClassLoader.getSystemClassLoader().getParent() ) );
should use the above URLClassLoader
to load the classes even if they are already loaded?