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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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:
  • 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,
WEBAPP_CLASSES, parent);

ClassLoader libDirLoader = createLoader(webAppDirPath, WEBAPP_LIB,
classesDirLoader);

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) {
; // IGNORE
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..
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not post the same question to several forums, like you have done here. It leads to duplicated effort, and doesn't really increase your audience. Let's continue the discussion in the other thread, since this is a servlet-related topic.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
    Bookmark Topic Watch Topic
  • New Topic