2

I have converted existing web project into Maven, technologies used are : Struts 2, Hibernate. A listener extends javax.servlet.ServletContextListener, which has a entry in web.xml as follows:

<listener>
    <listener-class>com.tcs.weserv.util.Listener</listener-class>
  </listener>

Maven project build successfully, but when I try to start server, it throw an exception:

java.lang.ClassNotFoundException: package.util.Listener

All dependencies are defined properly in pom.xml, listener is available at given location, still it throws ClassNotFoundException.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Post a stacktrace() – Roman C May 24 '17 at 11:09
  • java.lang.ClassNotFoundException: com.tcs.weserv.util.Listener at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532) – Namrata Choudhary May 24 '17 at 11:14
  • Describe the steps you followed to "convert" your project. Please add the `... – Steve C May 24 '17 at 14:23
  • I have created a new maven web project, and then paste all java src to main/resources including struts.xml and hibernate.xml file. copied all webContent into webApp folder. Here is the build in pom.xml Workbench – Namrata Choudhary May 25 '17 at 05:27
  • To debug, I have created a demo project in maven , which will call the same listener(which is used to create hibernate session), at first time, it was giving the same ClassNotFoundException, but when I cleaned the project , It worked fine. same is not applicable in my actual project. – Namrata Choudhary May 25 '17 at 05:32
  • I am facing following error while starting the server : SEVERE: Error configuring application listener of class, followed by ClassNotFoundException – Namrata Choudhary May 26 '17 at 07:12

2 Answers2

1

Thanks all for your valuable comments. It helped to debug the problem. The actual problem is, Maven Project structure was wrong. When I created a Maven project by selecting archtype as webapp, the project structure contains only src/main/resources.

Since I am new to Maven, I thought this is actual project structure, and I copied all resources as well as java files into this folder. As a result, It was able to build the project without any error, but while deploying it on server, It is showing ClassNotFoundException.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

This is uknown listener. Cannot find any jar containing that listener. If you know how to create jar using some zip utility or jar executable then you can copy the class com.tcs.weserv.util.Listener.class to the jar file preserving the folder structure which is important to find the corresponding class.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • this listener is defined in my project itself. n before converting the project to maven , it was working fine. – Namrata Choudhary May 24 '17 at 11:22
  • If it is in the project, why it cant find it , n says ClassNotFoundExceprtion. – Namrata Choudhary May 24 '17 at 11:23
  • the file should be on classpath and the server should be able to load it – Roman C May 24 '17 at 11:33
  • see.. its a simple class which implements ServletContextListener. n It is in the project only. If i build the project , it should automatically in the war file. so.. while starting server, it is available on the specified path. I could not understand what do u mean by "file should be on classpath" ? – Namrata Choudhary May 24 '17 at 11:46
  • this is the thing that you don't understand. The class couldn't be loaded if it's not available. Verify your package or whatever you use to deploy your application to have consistent packaging that requires to build and start the web application. – Roman C May 24 '17 at 12:32
  • Hi @Roman, I have cross checked war file, and it contains the class. 30217 Thu May 25 18:09:48 IST 2017 WEBINF/classes/com/tcs/weserv/util/Listener.java – Namrata Choudhary May 26 '17 at 05:55
  • In the exception message you say that it is `package.util.Listener` – Roman C May 26 '17 at 08:50
  • last time, i haven't explained it in details, here package mean to com/tcs/weserv. The actual location of the listener is com.tcs.weserv.util – Namrata Choudhary May 26 '17 at 09:03
  • I didn't see where you explained it in details? You shouldn't substitute real values with imaginable. Nobody can understand this. I recommend you to check the `lib` folder to have the same jars as in working project. Maven is downloading jars and replaces them in the `lib` folder. You might have inconsistent project dependencies that failed on the server loading your class. See https://stackoverflow.com/q/34413/573032 – Roman C May 26 '17 at 09:14