The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Dynamic Extension

Advertisement

Advertisement

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.

Message:

Classloader

Posted by parag on April 17, 2001 at 6:55 AM

> Hello,

> I have written a custom class loader in using java 2.
> In my custom class loader I override the findClass() method
> in order to load classes from a jarConnection. All this is
> working fine. My question is how can I get my custom class
> loader to be used whenever another class is to be loaded. I
> have used a small bootstrap class that creates my custom loader
> and loads/invokes a main class. From there the main class creates other objects but it seems to use the primoridial class loader.

> I have also had my main class create an instance of my custom class and load/instantiate a new class. Then the new class
> creates another class using the new operator. I would expect
> my custom class loader to be invoked but alas it seems that the
> primordial class loader is used yet again.

> I have read your column and book excerpt and can't figure out
> why my custom class loader isn't be used 'automatically' after
> I have used it to create parent classes. Could there be a scope
> problem ( could the instance of my classloader be going out of scope ) I can't see how but have no other ideas.

> The way I see it I should never get a ClassNotFoundException
> without the findClass code I wrote being executed first ( assuming I loaded the application/class with my loader )

> any ideas?


Actually u need to over ride the loadclass method,if i am not mistaken.The basic theory behind classloader is that when u load a class using the custom classloader all the classes that the loaded class needs are supposed to be loaded by the custom class loader, this includes the System classes also .Since all the classes are associated with a classloader with which they are loaded and it is this classloader that gets the chance to load the classes needed by the loaded class, hence u shouls be able to load system classes by calling findsystemclass method.I am attching a piece of code that just does that.

import java.io.*;
import java.util.*;
public class Rarpatvvvis extends ClassLoader
{
Hashtable cache=null;
ClassAdder adder=null;
public Rarpatvvvis()
{
cache=new Hashtable();
adder=new ClassAdder();
}
public synchronized Class loadClass(String name,boolean resolve) throws ClassNotFoundException
{ byte data[]=null;
Class cls=null;
try
{
System.out.println("trying to load from cache ");
cls=(Class)cache.get(name);
if(cls!=null)
{
System.out.println("class was loaded from cache ");
return cls;
}
System.out.println("trying to load class using System classloader ");
try
{
cls=findSystemClass(name);
if(cls!=null)
{
System.out.println("after loading System class "+name);
cache.put(name,cls);
return cls;
}
}
catch(Exception e)
{
System.out.println(e);
}

data=loadClassData(name);
System.out.println("data length "+data.length);
cls=defineClass(data,0,data.length);
System.out.println("name of the class "+cls.getName());
if(resolve)
{
resolveClass(cls);
}
cache.put(name,cls);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
throw new ClassNotFoundException();
}
return cls;
}

public byte[] loadClassData(String className)
{
return adder.getClassByte(className);
}
public static void main(String gars[])
{
try
{
System.out.println("inside main");
Rarpatvvvis classloader=new Rarpatvvvis();
Class c=classloader.loadClass(gars[0],true);
if(c==null)
{
System.out.println("class cannot be loaded ");
}
// testinf f=(testinf)c.newInstance();
// f.sayHello();
//Class m=classloader.loadClass("test.class",true);
//System.out.println("The name of the class "+c.getName());
}
catch(Exception e)
{
System.out.println(e);
}

}
}





Replies:

Sponsored Links



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