Custom ClassLoader which depends on more than one classloader
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
It is very urgent.
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?
It is very urgent.
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Geoffrey
Geoffrey
Sun Certified Programmer for the Java 2 Platform
posted 20 years ago
May a jar depend on another jar?
Depending on the usage of a jar, you might be independend from another one, aren't you?
I don't know much about classloaders, but this looks reasonable to me.
Beside this thinking, dependencies between jars are undirected, probably cyclic graphs, not trees. Isn't that a problem?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Now the class loader 6 which loads f.jar depends on a.jar, d.jar
May a jar depend on another jar?
Depending on the usage of a jar, you might be independend from another one, aren't you?
I don't know much about classloaders, but this looks reasonable to me.
Beside this thinking, dependencies between jars are undirected, probably cyclic graphs, not trees. Isn't that a problem?
avaya sahu
Greenhorn
Posts: 14
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Avaya.
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.
Avaya.
Geoffrey Falk
Ranch Hand
Posts: 171
1
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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!
Geoffrey
If you have to do this, then you could do something like
Better to reorganize your classloader hierarchy, though!
Geoffrey
Sun Certified Programmer for the Java 2 Platform
| Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ... Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







