Using separate classpaths for separate classes?

F

Frank Fredstone

I need to use classes that require conflicting classpaths, and I have
no access to source code.

Can I instantiate a class with one classpath in effect so that all the
classes it uses are resolved from their classpath, and then
instantiate a different class with a different classpath, so that all
the clasess it uses are resolved from that separate classpath?

Will it work to use URLClassLoader for this? It's not working for me,
but I think I might be making a mistake.

Should it work like this:

URLClassLoader clA = new URLClassLoader(new URL[] {
new URL("file:///somewhere/some-a.jar"),
});
Class ca = clA.loadClass("some.ClassA");
Object oa = ca.newInstance();

URLClassLoader clB = new URLClassLoader(new URL[] {
new URL("file:///somewhere/some-b.jar"),
});
Class cb = clB.loadClass("some.ClassB");
Object ob = cb.newInstance();

Then when some.ClassA instantiates some.OtherClass1 and
some.OtherClass2 (invisible to me), they will be loaded from
some-a.jar.

And, when some.ClassB instantiates some.OtherClass1 and
some.Otherclass2 (invisible to me), they will be loaded from
some-b.jar.

So, some.ClassA will never load anything from some-b.jar and
some.ClassB will never load anything from some-a.jar.
 
J

Jean-Francois Briere

Should work.
Make sure that some-a.jar and some-b.jar are NOT is the JVM class path.

Regards
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top