Custom ClassLoader is not working for my web application

Joined
Apr 22, 2008
Messages
3
Reaction score
0
Hi,
I have been working in a project where I have to write a web application custom class loader which will load the jar file from tomcat/webapps/<app_name>/module/<module_name>/lib/ directory.I would like to know how to write a custom web application class loader that would help me to load the jars from the above mentioned path well with loading the jar files from
tomcat/webapps/<app_name>/WEB-INF/lib directory.
I will not be able to keep my module related jars in WEB-INF/lib as there may be same jar files(say, jdom.jar) with different versions(say 0.9 and 1.1) and I need to load them dynamically.

I am getting the following error,
java.lang.NoClassDefFoundError: org/jdom/filter/AbstractFilter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1634)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:860)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1307)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2395)
at java.lang.Class.getMethod0(Class.java:2642)
at java.lang.Class.getMethod(Class.java:1579)

Please guide me on how to write a custom class loader for web application.Is there any setting I need to do?I am using Tomcat 5 with JDK 5.
Any comment will help me to go to a right direction.
:-(
Thanks,
-Tapas
 
Joined
Apr 22, 2008
Messages
3
Reaction score
0
Code

This is How, I was trying to Load but getting the problem,
File libDir = new File(//Full Path of the directory);

if(libDir.exists() && libDir.isDirectory()){

D.log("Aaaah.... Directory is found.");
File[] jarFiles = libDir.listFiles();
URL [] urls = new URL[jarFiles.length];
for(int count= 0;count < jarFiles.length;count++){
urls[count] = new URL("file://" + jarFiles[count].toString());
}
ClassLoader previous = Thread.currentThread().getContextClassLoader();
//PluginClassLoader pcl = new PluginClassLoader(urls,previous);
URLClassLoader pcl = (URLClassLoader)Thread.currentThread().getContextClassLoader();

//URLClassLoader pcl = URLClassLoader.newInstance(urls, Thread.currentThread().getContextClassLoader());

((StandardClassLoader)(pcl.getParent())).setDelegate(false);

for (int i = 0; i < jarFiles.length; i++){

D.log(jarFiles.toString());

Thread.currentThread().setContextClassLoader(pcl);
((StandardClassLoader)(pcl.getParent())).addRepository("file:" + jarFiles.toString());
JarFile jarFile = new JarFile(jarFiles);
Enumeration jarEnu = jarFile.entries();

while(jarEnu.hasMoreElements()){

JarEntry je = (JarEntry)jarEnu.nextElement();

if(je.toString().endsWith(".class")){

String className = je.toString().replace('/', '.').substring(0, je.toString().indexOf(".class"));
Class clazz = pcl.loadClass(className);
D.log(clazz.getName());
}
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top