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.
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.