Hi, I am working on a multiple application launcher. As it runs multiple applications simultaneously it requires that classes of each should be issolated from each other. There is no problem in implementing this. I have completed this using custom classloader. But the new requirement says that if two application refer to some common jar file then they should be shared. The main intension of this is to reduce memory usage. so keeping eye on new requiremtn we have decide to load each jar file using a new class loader. Take an example :
ClassLoader 1 loads a.jar
ClassLoader 2 loads b.jar (this depends on a.jar i.e classloader 1) so we set the parent of 2 as 1.
ClassLoader 3 loads c.jar (this depends on a.jar i.e classloader 1) so we set the parent of 3 as 1.
ClassLoader 4 loads d.jar
Now the class loader 5 which loads e.jar depends on a.jar, b.jar, c.jar
Now the class loader 6 which loads f.jar depends on a.jar, d.jar
Is it possible to reuse the 1,2,3 class loader in 5 and 6?
Yes.. Create a classloader to load the common jars, then pass it as the parent to your custom classloaders. When your custom classloader looks up something, it will first go to the parent and load the common jars from the parent classloader.
Hi Geoffrey, The solution you mentioned will work if the child classloader is depends on a single classloader(single parent), but in my case the child classloader is dependant on two or more classloader excpet its own jar file. There is no cyclic dpendancies.
Joined: Aug 17, 2001
This situation can get you in big trouble. For example, say you have class with the same name in b.jar and c.jar, that is used by e.jar. Which one gets loaded? If you go down this road, then I am afraid you will start getting strange ClassCastExceptions at some point, that will be harder and harder to debug..
If you have to do this, then you could do something like
Better to reorganize your classloader hierarchy, though!