Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Java Reflection question

Thread view: 
enzo.660@gmail.com - 10 Apr 2007 08:50 GMT
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!
Chris Uppal - 10 Apr 2007 15:01 GMT
> 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
enzo.660@gmail.com - 10 Apr 2007 18:36 GMT
> Things to think about:
>
[quoted text clipped - 7 lines]
> 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!");
   }

}
----------------------------------------------------------------------

i get the NoClassDefFoundError for SuperClass. If however, I remove
the inheritance(SuperClass) and just keep the SubClass, The loader
works fine.Do I have to separately load the SuperClass as well?
Chris Uppal - 11 Apr 2007 10:11 GMT
[me:]
> > What classloader should load the superclass ?
> >
[quoted text clipped - 6 lines]
> > 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
enzo.660@gmail.com - 11 Apr 2007 16:45 GMT
> You haven't really addressed the questions I suggested that you thought about.
>
[quoted text clipped - 7 lines]
> 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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.