0

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!!

Community
  • 1
  • 1
user2797422
  • 31
  • 2
  • 6
  • You have tagged this with `maven`. Are you using maven to build your WAR file? – Steve C Jul 25 '16 at 01:18
  • The way I created the WAR file was to do File --> Export --> WAR File from Eclipse. I am using maven for jar file/library management in Eclipse, but this may not be relevant to the problem at hand. Forgive me, I can remove maven as a tag if it is confusing things. – user2797422 Jul 25 '16 at 01:31

2 Answers2

0

After spending a significant amount of time attempting to get the above to work on Tomcat 7.0, I was unsuccessful. However, I installed Tomcat 8.0 (version 8.0.36), and used the exact same setup as I was using on Tomcat 7.0 (which didn't work for Tomcat 7.0), and it worked (on 8.0). Then after exporting my project to a WAR file and moving it to the Tomcat 8.0 webapps folder, it then immediately called contextInitialized() as I had been trying to get to work on 7.0. The workaround was simply to install and use Tomcat 8 instead of Tomcat 7. This still does not explain why the same procedure used in Tomcat 7.0 does not work. I have not seen any correspondence from anyone in response to my query that explains this behavior or anywhere on this website. If there is a solution to this problem other than using Tomcat 8.0, please post. Otherwise, this workaround works for me.

user2797422
  • 31
  • 2
  • 6
0

Can you check whether the classes were properly extracted in the webapps location (your case WEB-INF/classes/com/crafy/controller/AppContextListener.class).

It could be the class haven't been properly extracted to the location while deploying.