a problem caused by different classloader

04-08-21 wy123456789
I use the javassist to modify class when they load to JVM.

public Class loadClass(String className)


try {

//use javassist.Loader load class and modify them

return loader.loadClass(className);


catch(Exception ex) {


return null;



this mehtod return a Class object load by javassist.Loader@4a5ab2

Class cls=loadClass("demo.user");

I can execute the method of cls by reflect successful.but when i execute

User u=(User)cls.newInstance();

it throws a ClassCastException,I know it is caused by the different classloader bewteen

cls and u. u load by sun.misc.Launcher$AppClassLoader@92e78c.

How can I do to use the cls like the normal class : User u=(User)cls.newInstance()???

I use Thread.currentThread().setContextClassLoader(cls.getClassLoader());

but it is inefficiency.I can't set the (User u) use customer classloader.

2004-08-21 17:47