Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Ok. I won't pretend to know all the ramifications to using this ClassFinder class. To my limited understanding, it's a classloader working without knowledge of Tomcat's classloaders and designed to help you reference classes stored outside the webapp. As a test, try removing the ClassFinder class and moving a copy of these external classes to WEB-INF/classes of your webapp. If that works, you issue has to do with the way your independent classloader is ignoring Tomcat's classloader structure. Anyone else with more experience in custom classloaders care to comment on this? --David Armand Rock wrote: I personally have not changed any classpath settings. The classes are build using Eclipse SDK Version 3.1.1 with the Tomcat plugin from sysdeo (3.1.0.beta). I have noticed that the NoClassDefFoundError occurs on specific pages but only the first time the pages are loaded, everything else within the page works perfectly after I reload the page. In every instance that I have issues I'm using a Servlet which in turn makes use of the ClassFinder, it first calls a Java file (specific to the current command being run) followed by a redirection to a Jsp. As for ClassFileFinder here is the code. I can't find where this code was from but it was originally found online... I usually leave credit information at the top but this particuliar class did not contain any. public class ClassFileFinder extends ClassLoader { List cpath = new LinkedList(); Hashtable loadedClasses = new Hashtable(); public void addFile(File f) { if(f.isDirectory() || (f.isFile() f.canRead() f.getName().endsWith(.jar))) cpath.add(f); } public byte[] classData(String className) { String cname = className.replace('.', File.separatorChar) + .class; Iterator it = cpath.iterator(); while(it.hasNext()) { File f = (File)it.next(); try { if(f.isDirectory()) { File cFile = new File(f, cname); if(cFile.isFile()) { byte[] buf = new byte[(int)cFile.length()]; InputStream in = new FileInputStream(cFile); int off = 0, l; while((l = in.read(buf, off, buf.length - off)) 0) { off += l; if(off = buf.length) break; } in.read(buf); in.close(); return buf; } } else if (f.isFile()) { JarFile jar = new JarFile(f); JarEntry ent = jar.getJarEntry(cname); if(ent != null) { byte[] buf = new byte[(int)ent.getSize()]; int off = 0, l; InputStream in = jar.getInputStream(ent); while((l = in.read(buf, off, buf.length - off)) 0) { off += l; if(off = buf.length) break; } in.close(); return buf; } } } catch (IOException e) { } } return null; } public Class findClass(String className) throws ClassNotFoundException{ if (loadedClasses.get(className)!=null) { return Class.forName(className); } loadedClasses.put(className, className); byte[] data = classData(className); if(data == null) return getParent().loadClass(className); else return defineClass(className,data,0, data.length); } } -Original Message- From: David Smith [mailto:[EMAIL PROTECTED] Sent: December 7, 2005 7:47 AM To: Tomcat Users List Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext ...and that servlet-api.jar from tomcat
Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
...and that servlet-api.jar from tomcat is in the path. You wouldn't happen to be messing with the classpath at all would you? That will cause headaches. Best advise is to leave the classpath alone and let tomcat's classloader hierarchy handle finding classes. Thought 2: Tell me more about this ClassFinder class. It's not a part of Java's API and I'm wondering if it's not running it's own classloader independant of tomcat's classloader mechanism. --David John Poley wrote: Thank you for your comments thus far. I have been unable to solve my problem as well. I even went so far as to start with a machine with no development tools on it, downloading just the elements I required, and then trying to redeploy. I am fairly certain that there are no extra servlet jars anywhere, and that servlet-api.jar from tomcat is in the path. Still I face the java.lang.NoClassDefFoundError: javax/servlet/ServletContext error. I am using java 1.5.0_06 and and a 5.5 server. Well, trying to anyway! =) I'll place the error information at the very bottom of this email to avoid pushing down the previous responses too far. Any more ideas would be appreciated! - Original Message - From: Armand Rock [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Tuesday, December 06, 2005 12:44 PM Subject: RE: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext Hello David, I included all jar zip files on my computer, including j2ee.jar and I'm still having the problem. I have just noticed a weird behaviour though, if I try to reload the jsp right after the jsp fails the first time it seems to run without throwing the exception. If I restart Tomcat and try again the problem starts again. I could get this to work in theory by forcing it to run through the code once on startup but I shouldn't have to do that and I honestly would prefer getting it to work as it should. -Original Message- From: David Smith [mailto:[EMAIL PROTECTED] Sent: December 6, 2005 11:57 AM To: Tomcat Users List Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext I take it you also included j2ee.jar in that search? --David Armand Rock wrote: Hi, I'm getting the same error. I searched my entire computer for any jar/zip files containing javax.servlet.ServletContext and renamed all of them to .original so that they wouldn't be read by the JVM. I did this to all the files except for the file common/lib/servlet-api.jar I'm still getting the problem. I'm using java version 1.4.2_10 The code i'm using used to work under Orion version 1.4.5 (I'm now using Tomcat 5.5) The code that eventually throws the exception is basically: ClassFinder classFinder = new ClassFinder(); classFinder.addFile(/opt/classes/com/canlink/commands/); Class usrClass = classFinder.findClass(com.canlink.commands.TestClass); This line is what throws the exception: Method setCmdLog = usrClass.getMethod(setCmdLog, new Class[] {Boolean.class}); The stack trace is: SEVERE: Servlet.service() for servlet RunCmd threw exception java.lang.NoClassDefFoundError: javax/servlet/ServletContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at com.canlink.commands.RunCmd.service(RunCmd.java:240) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio n FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC h ain.java:173) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher . java:672) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDi s patcher.java:463) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatc h er.java:398) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatche r .java:301) at org.apache.jsp.web.Login_jsp._jspService(Login_jsp.java:67) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java: 3 22) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio n FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC h ain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.j a va:213
RE: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
I personally have not changed any classpath settings. The classes are build using Eclipse SDK Version 3.1.1 with the Tomcat plugin from sysdeo (3.1.0.beta). I have noticed that the NoClassDefFoundError occurs on specific pages but only the first time the pages are loaded, everything else within the page works perfectly after I reload the page. In every instance that I have issues I'm using a Servlet which in turn makes use of the ClassFinder, it first calls a Java file (specific to the current command being run) followed by a redirection to a Jsp. As for ClassFileFinder here is the code. I can't find where this code was from but it was originally found online... I usually leave credit information at the top but this particuliar class did not contain any. public class ClassFileFinder extends ClassLoader { List cpath = new LinkedList(); Hashtable loadedClasses = new Hashtable(); public void addFile(File f) { if(f.isDirectory() || (f.isFile() f.canRead() f.getName().endsWith(.jar))) cpath.add(f); } public byte[] classData(String className) { String cname = className.replace('.', File.separatorChar) + .class; Iterator it = cpath.iterator(); while(it.hasNext()) { File f = (File)it.next(); try { if(f.isDirectory()) { File cFile = new File(f, cname); if(cFile.isFile()) { byte[] buf = new byte[(int)cFile.length()]; InputStream in = new FileInputStream(cFile); int off = 0, l; while((l = in.read(buf, off, buf.length - off)) 0) { off += l; if(off = buf.length) break; } in.read(buf); in.close(); return buf; } } else if (f.isFile()) { JarFile jar = new JarFile(f); JarEntry ent = jar.getJarEntry(cname); if(ent != null) { byte[] buf = new byte[(int)ent.getSize()]; int off = 0, l; InputStream in = jar.getInputStream(ent); while((l = in.read(buf, off, buf.length - off)) 0) { off += l; if(off = buf.length) break; } in.close(); return buf; } } } catch (IOException e) { } } return null; } public Class findClass(String className) throws ClassNotFoundException{ if (loadedClasses.get(className)!=null) { return Class.forName(className); } loadedClasses.put(className, className); byte[] data = classData(className); if(data == null) return getParent().loadClass(className); else return defineClass(className,data,0, data.length); } } -Original Message- From: David Smith [mailto:[EMAIL PROTECTED] Sent: December 7, 2005 7:47 AM To: Tomcat Users List Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext ...and that servlet-api.jar from tomcat is in the path. You wouldn't happen to be messing with the classpath at all would you? That will cause headaches. Best advise is to leave the classpath alone and let tomcat's classloader hierarchy handle finding classes. Thought 2: Tell me more about this ClassFinder class. It's not a part of Java's API and I'm wondering if it's not running it's own classloader independant of tomcat's classloader mechanism. --David John Poley wrote: Thank you for your comments thus far. I have been unable to solve my problem as well. I even went so far as to start with a machine with no development tools on it, downloading just
Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
*sigh* it just had to be something simple, didn't it? Even right in front of me with the http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html page that someone suggested earlier. I put freemarker in the tomcat/shared/lib directory and all is well. I am having serious issues grasping this classloader thing, apparently =) Thanks for the help, all! - Original Message - From: Tim Funk [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Wednesday, December 07, 2005 9:01 PM Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext BINGO! freemarker.jar does not belong in the system classloader. It belongs in WEB-INF/lib. [ Quick rule of thumb - putting a lib in java/lib/ext will cause problems 99% of the time. ] -Tim John Poley wrote: And as for me, since we seem to be having similar issues, I cut out eclipse all together, worried that somehow it was cause some kind of issue. I have a .war file that was was exported by a colleague who is running the same .war within tomcat and having no trouble at all with it. I copy that .war file into the webapps directory of a freshly installed tomcat where I have changed no settings. I start tomcat, and then open a browser to either the simple HelloServlet file he included in the .war as a test or the actual servlet we are preparing- in both cases I get this same NoClassDefFoundError. I do not have j2ee on this system, and I have run a complete search for servlet.jar and j2ee.jar files in the hard drive and found none other than servlet-api.jar that lies within tomcat/common/lib. I am using a newly installed J2SE, JDK 5.0 Update 6, with no other java libraries installed. The only files I have added to java/lib/ext are freemarker.jar and and a mysql-connector jar. Am I missing something somewhere? - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
I take it you also included j2ee.jar in that search? --David Armand Rock wrote: Hi, I'm getting the same error. I searched my entire computer for any jar/zip files containing javax.servlet.ServletContext and renamed all of them to .original so that they wouldn't be read by the JVM. I did this to all the files except for the file common/lib/servlet-api.jar I'm still getting the problem. I'm using java version 1.4.2_10 The code i'm using used to work under Orion version 1.4.5 (I'm now using Tomcat 5.5) The code that eventually throws the exception is basically: ClassFinder classFinder = new ClassFinder(); classFinder.addFile(/opt/classes/com/canlink/commands/); Class usrClass = classFinder.findClass(com.canlink.commands.TestClass); This line is what throws the exception: Method setCmdLog = usrClass.getMethod(setCmdLog, new Class[] {Boolean.class}); The stack trace is: SEVERE: Servlet.service() for servlet RunCmd threw exception java.lang.NoClassDefFoundError: javax/servlet/ServletContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at com.canlink.commands.RunCmd.service(RunCmd.java:240) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh ain.java:173) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher. java:672) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDis patcher.java:463) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatch er.java:398) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher .java:301) at org.apache.jsp.web.Login_jsp._jspService(Login_jsp.java:67) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3 22) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh ain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja va:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja va:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126 ) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105 ) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java :107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processC onnection(Http11BaseProtocol.java:663) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.jav a:527) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWo rkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.jav a:684) at java.lang.Thread.run(Unknown Source) -Original Message- From: Tim Funk [mailto:[EMAIL PROTECTED] Sent: December 6, 2005 6:30 AM To: Tomcat Users List Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext See - http://tomcat.apache.org/faq/classnotfound.html Odds are you have your own servlet-api.jar somewhere in the webapp or system classpath and that is conflicting with the one in common/lib (installed by tomcat) -Tim John Poley wrote: Please forgive my intrusion if this is not the proper place to post a questoon of this sort. I am new to servlets, and am working on my first deployment- but I am running in to a problem that I can't find a solution to. I have installed Tomcat 5.5 and am using Eclipse (as well as a tomcat plugin) to manage my project. A colleague of mine sent me a war file of out working projected, which I imported to my IDE. I start tomcat, which seems to load properly, and attempt to run the project on the server- where I am faced with the following: javax.servlet.ServletException
Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
NoClassDefinition errors are normally thrown if the class or JAR isn't in the classpath, the only other time this happens is when the ClassLoader isn't able to load the class path because an exception is thrown on any static initialisers. (This is conjecture...) It may be the ServletContext has a static { ... } block that is throwing an error for some reason, the class can't be loaded and is therefore not available, hence the NoClassDefinition found error. However, I don't understand why your code is moaning about ServletContext when it doesn't look like you are anywhere near it. Kind Regards, Chris. On 6 Dec 2005, at 17:44, Armand Rock wrote: Hello David, I included all jar zip files on my computer, including j2ee.jar and I'm still having the problem. I have just noticed a weird behaviour though, if I try to reload the jsp right after the jsp fails the first time it seems to run without throwing the exception. If I restart Tomcat and try again the problem starts again. I could get this to work in theory by forcing it to run through the code once on startup but I shouldn't have to do that and I honestly would prefer getting it to work as it should. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Thank you for your comments thus far. I have been unable to solve my problem as well. I even went so far as to start with a machine with no development tools on it, downloading just the elements I required, and then trying to redeploy. I am fairly certain that there are no extra servlet jars anywhere, and that servlet-api.jar from tomcat is in the path. Still I face the java.lang.NoClassDefFoundError: javax/servlet/ServletContext error. I am using java 1.5.0_06 and and a 5.5 server. Well, trying to anyway! =) I'll place the error information at the very bottom of this email to avoid pushing down the previous responses too far. Any more ideas would be appreciated! - Original Message - From: Armand Rock [EMAIL PROTECTED] To: Tomcat Users List users@tomcat.apache.org Sent: Tuesday, December 06, 2005 12:44 PM Subject: RE: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext Hello David, I included all jar zip files on my computer, including j2ee.jar and I'm still having the problem. I have just noticed a weird behaviour though, if I try to reload the jsp right after the jsp fails the first time it seems to run without throwing the exception. If I restart Tomcat and try again the problem starts again. I could get this to work in theory by forcing it to run through the code once on startup but I shouldn't have to do that and I honestly would prefer getting it to work as it should. -Original Message- From: David Smith [mailto:[EMAIL PROTECTED] Sent: December 6, 2005 11:57 AM To: Tomcat Users List Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext I take it you also included j2ee.jar in that search? --David Armand Rock wrote: Hi, I'm getting the same error. I searched my entire computer for any jar/zip files containing javax.servlet.ServletContext and renamed all of them to .original so that they wouldn't be read by the JVM. I did this to all the files except for the file common/lib/servlet-api.jar I'm still getting the problem. I'm using java version 1.4.2_10 The code i'm using used to work under Orion version 1.4.5 (I'm now using Tomcat 5.5) The code that eventually throws the exception is basically: ClassFinder classFinder = new ClassFinder(); classFinder.addFile(/opt/classes/com/canlink/commands/); Class usrClass = classFinder.findClass(com.canlink.commands.TestClass); This line is what throws the exception: Method setCmdLog = usrClass.getMethod(setCmdLog, new Class[] {Boolean.class}); The stack trace is: SEVERE: Servlet.service() for servlet RunCmd threw exception java.lang.NoClassDefFoundError: javax/servlet/ServletContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at com.canlink.commands.RunCmd.service(RunCmd.java:240) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio n FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC h ain.java:173) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher . java:672) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDi s patcher.java:463) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatc h er.java:398) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatche r .java:301) at org.apache.jsp.web.Login_jsp._jspService(Login_jsp.java:67) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java: 3 22) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio n FilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC h ain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.j a va:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.j a va:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:12 6 ) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:10 5 ) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.jav a :107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868) at org.apache.coyote.http11
RE: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext
From: John Poley [mailto:[EMAIL PROTECTED] Subject: Re: Question concerning java.lang.NoClassDefFoundError: javax/servlet/ServletContext I even went so far as to start with a machine with no development tools on it, downloading just the elements I required, and then trying to redeploy. Those elements would be? The problem still looks like excess ServletContext classes being around. Since you're getting NoClassDefFoundError rather than ClassNotFoundException, there is a ServletContext already loaded from somewhere. If you happen to have j2ee.jar around, try getting rid of it. Have you looked at: http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html and compared that with what you're doing? - Chuck THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]