different versions of the same library

H

hilz

Hi all:
Is there a way to load two different versions of a jar library in the same
java application, and choose which one to use at runtime, with the ability
to switch between the two?
thanks
hilz
 
T

Tor Iver Wilhelmsen

hilz said:
Is there a way to load two different versions of a jar library in
the same java application, and choose which one to use at runtime,
with the ability to switch between the two?

Sort of, you need a custom ClassLoader. Also, you cannot "unload" a
class that has instances, since any new version of the class can be
incompatible.
 
H

hilz

Michael Borgwardt said:
Specifically, URLClassLoader will do the job.

I took another approach to solve my problem, and I won't need to do that
anymore,
but just out of curiousity, how would i unload a class?
in other words, if i still want to use the two versions of a library, then i
would use URLClassLoader to load a class from a jar file,
then if i want to use the other version of the class that is in another jar,
how would i unload the first one in order to load the second one? (assuming
i make sure i don't have any instances of that class at the time of
unloading).

thanks
hilz
 
O

Oscar kind

hilz said:
[...] just out of curiousity, how would i unload a class?
in other words, if i still want to use the two versions of a library, then i
would use URLClassLoader to load a class from a jar file,
then if i want to use the other version of the class that is in another jar,
how would i unload the first one in order to load the second one? (assuming
i make sure i don't have any instances of that class at the time of
unloading).

You don't: instead, you
- discard all references to the classloader for the first library
(including any objects loaded with it)
- create a new classloader for the second library
- create objects using that new classloader

Note that your starting class doesn't know about this other classloader;
the instantiated class (using the created classloaders) does.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top