*
The moose likes Java in General and the fly likes Custom ClassLoader which depends on more than one classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Custom ClassLoader which depends on more than one classloader" Watch "Custom ClassLoader which depends on more than one classloader" New topic
Author

Custom ClassLoader which depends on more than one classloader

avaya sahu
Greenhorn

Joined: Oct 16, 2001
Posts: 14
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.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
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


Sun Certified Programmer for the Java 2 Platform
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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?


http://home.arcor.de/hirnstrom/bewerbung
avaya sahu
Greenhorn

Joined: Oct 16, 2001
Posts: 14
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.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom ClassLoader which depends on more than one classloader
 
Similar Threads
Help please...jar problem
Loading same class by two different class loader (ClassCastException)
Loading jars as symlinks
JavaBeans sharing two or more utility classes.
jar classpath help