U
Ulrich Scholz
Dear all,
somehow I'm confused by ClassLoader. Please look at the code below.
First, I create a new ClassLoader cl1, more precisely, an instance of
the subclass MyDomainLoader of ClassLoader. Next, I create an instance
of class MyClass with Class.forName("MyClass", true, cl1). That works
fine.
The problem is: The new instance of MyClass seems to be loaded with
the class loader this.getClass().getClassLoader() and _not_ with the
new class loader cl1 that I provided.
What is wrong here?
Here's the story:
I want to get instances of different classes that have the same name. I
try this the following way
- write the java-code to a file (it's always the same file, it gets
overwritten)
- compile the java file into a class file
- create a new instance of MyDomainLoader which extends ClassLoader
- get an instance of the class by using the new ClassLoader
The problem now is that they don't use the class loaders as expected.
Thanks,
Ulrich
ClassLoader cl1 = new MyDomainLoader();
assertFalse(cl1.equals(this.getClass().getClassLoader())); //
it's a new one!
Class domainClass1 = null;
try
{
domainClass1 = Class.forName("MyClass", true, cl1);
}
catch(Exception e)
{
throw(new PPException(INTERNAL_ERROR, e.toString())); //
never happens
}
ClassLoader dl1 = domainClass1.getClassLoader();
// both succeed but should fail!
assertTrue(dl1.equals(this.getClass().getClassLoader()));
assertFalse(dl1.equals(cl1));
// an inner class
final class MyDomainLoader extends ClassLoader
{
public MyDomainLoader()
{
super();
}
}
somehow I'm confused by ClassLoader. Please look at the code below.
First, I create a new ClassLoader cl1, more precisely, an instance of
the subclass MyDomainLoader of ClassLoader. Next, I create an instance
of class MyClass with Class.forName("MyClass", true, cl1). That works
fine.
The problem is: The new instance of MyClass seems to be loaded with
the class loader this.getClass().getClassLoader() and _not_ with the
new class loader cl1 that I provided.
What is wrong here?
Here's the story:
I want to get instances of different classes that have the same name. I
try this the following way
- write the java-code to a file (it's always the same file, it gets
overwritten)
- compile the java file into a class file
- create a new instance of MyDomainLoader which extends ClassLoader
- get an instance of the class by using the new ClassLoader
The problem now is that they don't use the class loaders as expected.
Thanks,
Ulrich
ClassLoader cl1 = new MyDomainLoader();
assertFalse(cl1.equals(this.getClass().getClassLoader())); //
it's a new one!
Class domainClass1 = null;
try
{
domainClass1 = Class.forName("MyClass", true, cl1);
}
catch(Exception e)
{
throw(new PPException(INTERNAL_ERROR, e.toString())); //
never happens
}
ClassLoader dl1 = domainClass1.getClassLoader();
// both succeed but should fail!
assertTrue(dl1.equals(this.getClass().getClassLoader()));
assertFalse(dl1.equals(cl1));
// an inner class
final class MyDomainLoader extends ClassLoader
{
public MyDomainLoader()
{
super();
}
}