• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Creating Classloader for a servlet container

Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All Ranchers,

I am getting while creating classloader for my servlet container.
What I have done is that i have created a URLClassloader as bootstrap loader for all container classes.
Now whenever i try to create a new classloader for a deployed web application taking this bootstrap classloader as parent.
The web app classloader is again a URLClassloader which is gives first priorty to the classes availble in WEB-INF/classes to the WEB-INF/lib classes as shown below

final static String WEBAPP_CLASSES = "WEB-INF/classes";

final static String WEBAPP_LIB = "WEB-INF/lib";

public static ClassLoader forWebApp(File webAppDirPath) {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
ClassLoader classesDirLoader = createLoader(webAppDirPath,

ClassLoader libDirLoader = createLoader(webAppDirPath, WEBAPP_LIB,

return libDirLoader;

private static URLClassLoader createLoader(File webAppDirPath,
String forDir, ClassLoader parent) {
String path;
URL urls[] = new URL[] {};
try {
path = webAppDirPath.getCanonicalPath() + "/" + forDir;
urls = createClassPathURLs(path);
} catch (IOException e) {
; // IGNORE and return not a valid path
log.error("Invalid DD path [" + webAppDirPath + "]!", e);

return new URLClassLoader(urls, parent);

private static URL[] createClassPathURLs(String path) {
URL urls[] = new URL[] {};
File webAppCls = new File(path);
if (webAppCls.exists() && webAppCls.isDirectory()) {
File conLibFiles[] = webAppCls.listFiles(new FileFilter() {
public boolean accept(File file) {
String fileName = file.getName().toLowerCase();
boolean isDir = file.isDirectory();
boolean isJar = fileName.endsWith(".jar");
boolean isZip = fileName.endsWith(".zip");
return !isDir && (isJar || isZip);

urls = new URL[conLibFiles.length + 1];
try {
urls[0] = webAppCls.toURL();
} catch (MalformedURLException e) {
; // IGNORE can't come now
for (int i = 0; i < conLibFiles.length; i++) {
try {
urls[i + 1] = conLibFiles[i].toURL();
} catch (MalformedURLException e) {
log.error("Invalid Class Path URL found [" + conLibFiles[i]
+ "]", e);
return urls;

I am facing following problem
i.e The Web APP is having a class called a.b.Test
that is available in WEB-INF/classes dir and its referring to some
other class x.y.SomeJarClass in WEB-INF/lib dir.
But a java.lang.NoClassDefFoundError: is thrown for x.y.SomeJarClass in WEB-INF/lib dir.

thanking u in advance..
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not a Servlet question, moving to the Java in General (advanced) forum.

Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are not so lazy you can study my code used in TJWS J2EE servlet deployment. I believe it has no such problem.
I'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic