Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set a new Custom ClassLoader?

 
moh maew
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Case:
-----
I want to use POI package ver 1.5.1 and POI package ver 2.0 RC in my
application
Problem:
-------
When my application start the classloader load the POI package ver
1.5.1, i want to destroy that classloader at runtime and set my new
custom classloader that define the new POI package ver 2.0 RC
Need:
-----
I want any suggestions from anyone to help me in find a manner to
destory the old classloader and a manner to set the new classloader at
runtime
Thanks in Advance,
 
Scotty Sinclair
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a classloader is no longer referenced then it will be grabage collected.
If any classes loaded by that classloader is no longer referenced then that class will be unloaded from the JVM.
I am having a similar problem, I am destroying all references to objects created by one classloader and then creating another classloader and loading the classes freshly. But the classes aren't unloading in my application even though I got them unloading in a test example.
here is some code which I wrote to test the unloading of classses

GetValue is just some class which we load dynamically using the URLClassloader. If you run the JVM in verbose mode you should see the classes get unloaded after the grabage collect
 
moh maew
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your beautiful answer,
but your code help me in loading only one class, i want to load the whole package with all of its classes.
Any Suggestions
Thanks in Advance,
 
Scotty Sinclair
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work just like when compiling, i.e. a chain reaction. If a class is referenced by the class you are compiling then it is compiled too. It should be the same for class loading, any required classes will be loaded by the same class loader which loaded the referring class.
Or you could create a class for hadling the library which makes sure the proper class loader is used or something.
class Library {
private ClassLoader c;
public Class getClass();
}
I have never tried any of this, they are just suggestions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic