This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Classloader for a servlet container

 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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,
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..
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a Servlet question, moving to the Java in General (advanced) forum.

Dave
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • 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.
http://cvs.sourceforge.net/viewcvs.py/tjws/tjws/src/rogatkin/web/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic