*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating Classloader for a servlet container" Watch "Creating Classloader for a servlet container" New topic
Author

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,
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..


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

Joined: Mar 06, 2001
Posts: 13459

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

Dave
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.
http://cvs.sourceforge.net/viewcvs.py/tjws/tjws/src/rogatkin/web/


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
 
Don't get me started about those stupid light bulbs.
 
subject: Creating Classloader for a servlet container
 
Similar Threads
How to Load Serialized Plugins?
Creating Classloader for a servlet container
code redundancy
what is WebappLoader.java ?
New Java, old CORBA