The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Dynamic Extension



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


custom class loader woes...

Posted by Mike Red on January 08, 20100 at 12:07 PM

My problem is that I'm storing serialized file and class file in
a database. When I try to load the serialzed file, my customised
ClassLoader is invoked which tries to loads the required class
from the database (naturally the class wanted is not in the
classpath) . However I get a NoClassDefFoundError, I wonder why.
I have also overridden the resolveClass() of ObjectInputStream
as required, from where the findClass() of my custom classloader
is invoked.
My Class Loader code is as shown below. Any inputs as to what
should be done would be highly appreciated ....

thanks in advance ......

public class MyClassLoader extends java.lang.ClassLoader {

private HashMap cache = null;

public synchronized Class loadClass(String className,
boolean resolveIt) throws ClassNotFoundException {

/* check the local cache of classes */

/* check with the primordial class loader */

result = super.loadClass(className , true);
if(result != null){ return result; }//returning the class

/* call findClass() to fetch the class */

result = findClass(className);

/* links the specified if asked to */
if(resolveIt) { resolveClass(result); }//resolved !

return result; }//loadClass

private byte[] getClassFromDB(String className)
throws ClassNotFoundException {

byte[] result = nul;
/* go to the database and get the class in a byte[] */

return result; }//getClassFromDB

protected Class findClass(String className) throws
ClassNotFoundException {

Class result = null;
byte[] classData = null;

/*now the great database is looked on upon*/
classData = getClassFromDB(className);
if(classData == null) {
throw new ClassNotFoundException(); }//got from db

/* this method converts the array of bytes into an instance of
Class */
/* THE NOCLASSDEFFOUNDERROR is thrown in the next line ######## */

result = defineClass(className,classData,0,classData.length);

if(result == null) { throw new ClassFormatError(); }//if

return result; }//findClass


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us