Java Reflection question

E

enzo.660

Hey!
I'm using the Reflection API

I load a class called 'SubClass' which exists in a directory called
'subdir' at run-time from my program
--------------------------------------------------------------------
----------
CustomClassLoader loader = new CustomClassLoader();
Class classRef = loader.loadClass("SubClass");
----------------------------------------------
--------------------------------

class CustomClassLoader extends ClassLoader. I have defined
'findClass(String className)' method in CustomClassLoader.

This is what 'findClass(String className)' returns:
defineClass (className,byteArray,0,byteArray.length);

'byteArray' is of type byte[] and has the contents of subdir/SubClass.

the problem:
-----------------
The program runs fine if SubClass does not extend any class.
If however, SubClass extends another class, the program throws a
NoClassDefFoundError. How does one take care of the inheritance here?

Help appreciated in advance..
Thanks!
 
C

Chris Uppal

The program runs fine if SubClass does not extend any class.
If however, SubClass extends another class, the program throws a
NoClassDefFoundError. How does one take care of the inheritance here?

There shouldn't be a problem here (and remember that even if you don't declare
SubClass to extend anything, then it is still extending Object -- so your code
/can/ load classes with superclasses).

Things to think about:

What classloader should load the superclass ?

If that is one of your custom classloaders, is it looking in the right place ?

If that is not the same as the classloader which loads "SubClass", is the
intended classloader set as the parent of the custom classloader ?

What are the package names of the various classes ? Does your classloader know
how to map package names onto directory names ?

-- chris
 
E

enzo.660

Things to think about:

What classloader should load the superclass ?

If that is one of your custom classloaders, is it looking in the right place ?

If that is not the same as the classloader which loads "SubClass", is the
intended classloader set as the parent of the custom classloader ?

What are the package names of the various classes ? Does your classloader know
how to map package names onto directory names ?


Hey Thanks!

it is a customized class loader. The file to be loaded(SubClass.java)
is in a directory (subdir). class SubClass extends class 'SuperClass'
also defined in SubClass.java

something like this:

-----------------------------------------------------------------
class SuperClass {


public int cadence;
public int gear;
public int speed=7;


public SuperClass(int startCadence, int startSpeed, int startGear)
{
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}



}

public class SubClass extends SuperClass {


public int seatHeight;


public SubClass(int startHeight, int startCadence, int startSpeed,
int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}


public void disp() {
System.out.println(" Speed : " + speed);
System.out.println("hi!");
}

}
 
C

Chris Uppal

(e-mail address removed) wrote:

[me:]
What classloader should load the superclass ?

If that is one of your custom classloaders, is it looking in the right
place ?

If that is not the same as the classloader which loads "SubClass", is
the intended classloader set as the parent of the custom classloader ?

What are the package names of the various classes ? Does your
classloader know how to map package names onto directory names ?
[...]
it is a customized class loader. The file to be loaded(SubClass.java)
is in a directory (subdir). class SubClass extends class 'SuperClass'
also defined in SubClass.java


You haven't really addressed the questions I suggested that you thought about.

For instance (only one example), /which/ classloader is expected to load
SuperClass ? (SuperClass, BTW, will be loaded automatically as a side-effect
of attempting to load SubClass). Where is that classloader /supposed/ to be
looking for the .class file ? Where /is/ the .class file ?

Remember that it's your responsibility (or rather, it's your classloader's
responsibility) to take the class name, and deduce where to find the
classfile -- the classloading framework provides /no/ help with that (it can't,
because there is no defined connection between class names and file names).

-- chris
 
E

enzo.660

You haven't really addressed the questions I suggested that you thought about.
For instance (only one example), /which/ classloader is expected to load
SuperClass ? (SuperClass, BTW, will be loaded automatically as a side-effect
of attempting to load SubClass). Where is that classloader /supposed/ to be
looking for the .class file ? Where /is/ the .class file ?

Remember that it's your responsibility (or rather, it's your classloader's
responsibility) to take the class name, and deduce where to find the
classfile -- the classloading framework provides /no/ help with that (it can't,
because there is no defined connection between class names and file names).

nevermind Chris. i figured it out on Sun forums. I had to load the
SuperClass separately!

Thanks!
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top