Friday 15 July 2011

java - define new class in Tomcat -



java - define new class in Tomcat -

i want load new class(not loaded) in tomcat byte array. have reference tomcat application class loader. declare custom classloader as

class customclassloader extends classloader{ public customclassloader(classloader classloader) { super(classloader); } public class loadthisclass(string name){ //get .class byte array in data. class clazz = defineclass(name, data, 0, data.length, null); resolveclass(clazz); homecoming clazz; } }

i using classloader as

customclassloader customclassloader = new customclassloader(getclass().getclassloader()); /* print getclass().getclassloader() , output webappclassloader context: /test delegate: false repositories: /web-inf/classes/ ----------> parent classloader: org.apache.catalina.loader.standardclassloader@52e99642 */ class clazz = customclassloader.loadthisclass("com.class1"); //print clazz.getmethods, print new method.

when seek check class follows

class.forname("com.class1");

i getting classnotfoundexception , class not accessible other classes in web application.

how can define new class in tomcat web application accessible other classes in web application..

it may not plenty don't need override findclass in classloader?

protected class findclass(string name) throws classnotfoundexception

finds class specified binary name. method should overridden class loader implementations follow delegation model loading classes, , invoked loadclass method after checking parent class loader requested class. the default implementation throws classnotfoundexception.

response comment:

the prohibited bundle name : java.lang implies trying load class in bundle space of java.lang own class loader. either class loader or class trying load in bundle of java.lang?

if not class loader may trying load much. within override of findclass should check if in domain , throw if not.

protected class<?> findclass(string name) { if(!name.startswith("com.your.package")) throw new classnotfoundexception(); }

java tomcat tomcat7 classloader

No comments:

Post a Comment