Custom class loader

S

spaceman

Hi,

How can I create a custom class loader which loads classes only from a
specific jar?
e.g. in my web apps web-inf/lib I have a.jar and b.jar and I'd like to
have two separate class loaders for each of them. Is it possible?
As these jars are libraries and do not have a main() etc.. or any other
entry point, how can I set the class loader for every class in the
jars?

Any advice is highly appreciated.
Thanks
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

spaceman said:
How can I create a custom class loader which loads classes only from a
specific jar?
e.g. in my web apps web-inf/lib I have a.jar and b.jar and I'd like to
have two separate class loaders for each of them. Is it possible?
As these jars are libraries and do not have a main() etc.. or any other
entry point, how can I set the class loader for every class in the
jars?

WEB-INF/lib ? No !

Because they will be loaded by a parent classloader.

If the two jar files are outside of normal classloader search
path it is simple.

URLClassLoader cl = new URLClassLoader(urlofjar);
X o = (X)Class.forName(clsnam, true, cl).newInstance();

Arne
 
S

spaceman

Thank You.
WEB-INF/lib ? No !
Let me be more specific. The problem I have is that both a.jar and
b.jar have some classes which are the same ( duplicated ). If I put
them toghether in WEB-INF/lib the classloader gets messed up due to the
duplicate classes.
Because they will be loaded by a parent classloader.

If the two jar files are outside of normal classloader search
path it is simple.

URLClassLoader cl = new URLClassLoader(urlofjar);
X o = (X)Class.forName(clsnam, true, cl).newInstance();
This seem to be a good possibility however as there is no entry point
and there are a lot of classes, I'm not sure how I can control the
creation of each class and use my own class loader.
Also how can I have the web server load the jars at some point if they
are not in web-inf/lib?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

spaceman said:
Let me be more specific. The problem I have is that both a.jar and
b.jar have some classes which are the same ( duplicated ). If I put
them toghether in WEB-INF/lib the classloader gets messed up due to the
duplicate classes.

Yes.

You need two custom classloaders one for each jar and the jars
must not ne in path for parent classloaders.
This seem to be a good possibility however as there is no entry point
and there are a lot of classes, I'm not sure how I can control the
creation of each class and use my own class loader.
Also how can I have the web server load the jars at some point if they
are not in web-inf/lib?

The Class.forName code above explicit specify classloader.

The URL for the classloader can be any URL including file
URL's.

Note that the deployment of you app will become a bit tricky, but
that is how it has to be.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top