Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Custom class loader

Thread view: 
spaceman - 25 Sep 2006 22:24 GMT
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
Arne Vajhøj - 26 Sep 2006 03:36 GMT
> How can I create a custom class loader which loads classes only from a
> specific jar?
[quoted text clipped - 3 lines]
> 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
spaceman - 26 Sep 2006 14:41 GMT
> > How can I create a custom class loader which loads classes only from a
> > specific jar?
[quoted text clipped - 3 lines]
> > entry point, how can I set the class loader for every class in the
> > jars?

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.
>
[quoted text clipped - 3 lines]
> 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?

> Arne
Arne Vajhøj - 30 Sep 2006 20:28 GMT
>> 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.

Yes.

You need two custom classloaders one for each jar and the jars
must not ne in path for parent classloaders.

>> URLClassLoader cl = new URLClassLoader(urlofjar);
>> X o = (X)Class.forName(clsnam, true, cl).newInstance();
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.