File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Creating Classloader for a servlet container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating Classloader for a servlet container" Watch "Creating Classloader for a servlet container" New topic

Creating Classloader for a servlet container

Amitkumar Dhama
Ranch Hand

Joined: Feb 14, 2005
Posts: 56
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..

SCJP 1.4, SCWCD 1.4, M.Tech(CSE)
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

This is not a Servlet question, moving to the Java in General (advanced) forum.

dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
If you are not so lazy you can study my code used in TJWS J2EE servlet deployment. I believe it has no such problem.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: Creating Classloader for a servlet container
jQuery in Action, 3rd edition