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) {

ex.printStackTrace();

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.

banq
2004-08-21 17:47
研究一下classloader策略,注意保护你环境的classloader,这篇文章供参考:

http://www.jdon.com/jive/article.jsp?forum=91&thread=15456

猜你喜欢