I am running Tomcat 7.0.70 and Eclipse Kepler on a Windows 64 bit machine, Java 7. My software works perfectly when I run it from Eclipse, but I am getting the following error only when I deploy my project to Tomcat, after exporting my project to a WAR file:
SEVERE: Error configuring application listener of class com.crafy.controller.AppContextListener java.lang.ClassNotFoundException: com.crafy.controller.AppContextListener.
AppContextListener is my own file that implements ServletContextListener. Here is part of the file, of which I removed everything except for contextInitialized(), and there, I removed everything except for a print statement (contextInitialized() is not getting called at all):
package com.crafy.controller;
@WebListener
public class AppContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
SessionFactory sessionFactoryInst = null;
System.out.println("Database connection initialized for Application.");
}
}
When I run it from Eclipse, the entire application works perfectly (right click "Tomcat v7.0 Server at localhost" and click Start). Separately, where I get the above error is when I try to use the WAR file. To get the WAR file, I exported from Eclipse (by going to File -> Export -> WAR File). I then placed the WAR file into the folder: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps. When I go to the IP address (http://192.168.1.154:8080/crafy), the site comes up, but I am not able to access the DB, which is initialized by my AppContextListener. However, it doesn't initialize, because the class is not found.
Here are the relevant portions of my application specific web.xml (located in C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\crafy\WEB-INF\web.xml):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>crafy</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>videoSession</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>videoSession</servlet-name>
<url-pattern>/videoSession.jsp</url-pattern>
<url-pattern>/videoSession.html</url-pattern>
</servlet-mapping>
<resource-env-ref>
<description>Transactions</description>
<resource-env-ref-name>UserTransaction</resource-env-ref-name>
<resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
</resource-env-ref>
<listener>
<listener-class>com.crafy.controller.AppContextListener</listener-class>
</listener>
</web-app>
The AppContextListener.class file does exist at: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\crafy\WEB-INF\classes\com\crafy\controller
I have tried checking and changing the path in the to multiple different things and have tried moving the AppContextListener.class file to different locations, as suggested in Apache: Error configuring application listener. I have also tried deleting the server on the Eclipse IDE creating it again, then exporting to WAR again as suggested in the same link. That did not resolve anything. I also tried "Clean Tomcat Work Directory..." and "Clean..." from the Tomcat server in Eclipse (see ClassNotFoundException with ServletContextlistener and ServletContextListener SEVERE: Error configuring application listener of class marktest.Config which suggests both cleans) and then exporting to war again. I also tried the multiple suggestions in this post: SEVERE: Error configuring application listener of class org.apache.catalina.deploy.ApplicationListener. None were successful.
I know that application specific web.xml is being accessed, because if I remove it altogether, then when I go to the URL, it goes to the index.jsp as opposed to the home.jsp as specified in the web.xml. So, there must be something wrong with the class path that I am providing in the tag, however, I seem to have tried everything but still cannot find it. Like I said, everything works fine when I run the server from Eclipse. I tried mirroring the same folder structure as Eclipse uses. Specifically, the folder structure for the Eclipse project is:
base/crafy/WebContent/WEB-INF/ ----> contains web.xml and contains jsp and lib folders base/crafy/target/classes/com/crafy/controller ----> contains AppContextListener.class
However, when the war file is deployed to Tomcat, the folder structure is:
C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/crafy/WEB-INF/ ----> contains web.xml and contains jsp and lib AND classes folders so that the AppContextListener.class file is located in webapps/crafy/WEB-INF/classes/com/crunchify/controller
The relative locations are slightly different between Eclipse and when deployed directly to Tomcat, which I have tried to make changes to make them equivalent, but it hasn't seemed to help.
I was also previously using Tomcat 7.0.68, which had the same problems, which prompted me to intall the latest Tomcat version to 7.0.70, but that also has yielded the same exact problem.
Does anyone have any suggestions as to how I can debug this further? Any help would be most appreciated. I have been spent many hours trying to diagnose this problem, please help!!