File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to set a new Custom ClassLoader? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to set a new Custom ClassLoader?" Watch "How to set a new Custom ClassLoader?" New topic
Author

How to set a new Custom ClassLoader?

moh maew
Greenhorn

Joined: Jan 20, 2004
Posts: 3
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

Joined: Jun 26, 2003
Posts: 17
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

Joined: Jan 20, 2004
Posts: 3
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

Joined: Jun 26, 2003
Posts: 17
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to set a new Custom ClassLoader?