1

I have a servlet application, which I deploy on a Tomcat server. I use Maven to resolve dependencies and Hibernate for database connection. Currently I have following problem when running application:

java.lang.ClassNotFoundException: org.dom4j.io.STAXEventReader

I have added Maven dependency:

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

It appeared in the deployed WAR file, and particularly the org.dom4j.io.STAXEventReader can be found in a jar that is packed into WAR. Yet, I get very same class not found exception. I have spent several days already googling for the solution, yet haven't found anything. Has anybody faced something like that?

Before that I had a similar problem with HibernateException, but it disappeared (I hope) as I changed used Hibernate version (and that's really strange!).

Update

I attach the full stack trace:

SEVERE: Allocate exception for servlet com.prefserver.servlets.DispatcherServlet
java.lang.ClassNotFoundException: org.dom4j.io.STAXEventReader
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
    at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:87)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:124)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
    at com.prefserver.dao.PlayerDaoImpl.createSessionFactory(PlayerDaoImpl.java:24)
    at com.prefserver.dao.PlayerDaoImpl.<clinit>(PlayerDaoImpl.java:33)
    at com.prefserver.utility.RegistrationChecker.<init>(RegistrationChecker.java:17)
    at com.prefserver.servlets.DispatcherServlet.<clinit>(DispatcherServlet.java:43)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:116)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:724)

Output of the mvn dependency:tree -Dverbose -Dincludes=dom4j

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ PrefServer ---
[INFO] com.prefserver:PrefServer:war:0.0.1-SNAPSHOT
[INFO] +- org.hibernate:hibernate-core:jar:5.0.0.Final:compile
[INFO] |  \- (dom4j:dom4j:jar:1.6.1:compile - omitted for duplicate)
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.0.0.Final:compile
[INFO] |  \- (dom4j:dom4j:jar:1.6.1:compile - omitted for duplicate)
[INFO] \- dom4j:dom4j:jar:1.6.1:provided (scope not updated to compile
Andy Silver
  • 155
  • 3
  • 8
  • are you using `Eclipse`?. How do you create the `WAR`. `mvn package`? – diyoda_ Aug 29 '15 at 09:00
  • The jar is present in the dom4j 1.5 and 1.6.1, might be while building it was not getting build, so please try to re-build with `mvn -X clean install -n` command in command prompt. – SaviNuclear Aug 29 '15 at 09:01
  • I am using Eclipse. I create the WAR with eclipse, running maven BUILD target. It worked for a long time till something has changed - don't know exactly, I made some minor changes in the code. Ok, I'll try to build from the command line. – Andy Silver Aug 29 '15 at 09:06
  • 2
    You should put full of stacktrace into your question (like this: http://stackoverflow.com/q/9002082/3728901 ) for more information. – Vy Do Aug 29 '15 at 09:09
  • Did you add any libraries to Tomcat's lib directories. It is possible that whatever is calling `dom4j` was actually loaded with one of the other classloaders in Tomcat (not your application's) and therefore can't find the jar inside the war. – Aleksandr Dubinsky Aug 29 '15 at 09:10
  • Also check if there are any dependency conflicts. There's a tool for it in Netbeans, not sure about Eclipse. You'll need to exclude any conflicting dependencies in your other dependencies, and hope that a single version will be compatible for both callers. – Aleksandr Dubinsky Aug 29 '15 at 09:11
  • @AndySilver you can specify `provided` for this dependency and see how that goes. – diyoda_ Aug 29 '15 at 09:14
  • @SaviNuclear I have tried this command line, unfortunately same thing. No, I haven't added anything except mysql-connector-java-5.1.35-bin.jar – Andy Silver Aug 29 '15 at 09:21
  • please provide the pom.xml file, so that we can dig the issue efficiently. – SaviNuclear Aug 29 '15 at 09:24
  • Here is a link to pom.xml code: [pom.xml](http://codeshare.io/VGjOV) – Andy Silver Aug 29 '15 at 09:27
  • @Diode, I have set the scope to provided, and somewhy the jar was not packed into WAR at all. So, same problem. – Andy Silver Aug 29 '15 at 09:49
  • @Andy, `provided` scope means it won't be contained in the war, rather it should be provided by the container(here it is tomcat). I think, @Diode was trying to eliminate multiple jars in same classpath problem – appu Aug 29 '15 at 09:51
  • @AndySilver Can you make this command work form your CMD `mvn dependency:tree -Dverbose -Dincludes=dom4j` and see which uses this jar as a dependancy. And then let us know. I am not 100% sure that this is the exact command that you have to execute. you will have to figure it out :). [Here](http://techidiocy.com/maven-dependency-version-conflict-problem-and-resolution/) is the article. – diyoda_ Aug 29 '15 at 09:56
  • yeah. sorry, I am a newbie with maven and only faced compile scope before. @Diode I put output of the command you suggest into the question body. – Andy Silver Aug 29 '15 at 10:16
  • Dependency tree tells that if you `haven't specified the explicit dependency for dom4j` in your pom.xml for dom4j, it will get the same version of dom4j `transitively through hibernate-* artifacts`. BTW, now I observed that your pom.xml is not viewable at http://codeshare.io/VGjOV. Can you please correct it ? – appu Aug 29 '15 at 11:37
  • Once I get the pom.xml, I'll try to simulate your scenario in my end – appu Aug 29 '15 at 11:39
  • Oh, I fixed it, sorry. [pom.xml](http://codeshare.io/VGjOV) – Andy Silver Aug 29 '15 at 12:12
  • Well, I don't know what has happened - I just removed the application from the server using Eclipse, then re-run it, and everything started to work like a charm... I am really sorry I didn't get the point of what has actually fixed everything up. The logs are empty since 3 of august, guess I have wrong settings for logging. Yet, thank you very much everyone for your time spending. If I understand the reason of what was going on, I'll post it here. – Andy Silver Aug 29 '15 at 12:45
  • Oh, NP ! Glad to hear that the issue got resolved. Keep posted if you get the exact issue, so that it will be helpful for someone else. – appu Aug 29 '15 at 12:51
  • @Andy one more input, it is not the prefered method to use `systemPath` and `system` scope in dependency. Ideally project should be built from any mavenized system. But specifying `systemPath` will break that rule. So make the `PrefCommon.jar` available in remote or local repo and use maven co-ordinates(groupId:artifactId:package:version) to resolve it. That will be a good pattern – appu Aug 29 '15 at 13:04
  • @AndySilver good luck – diyoda_ Aug 29 '15 at 20:47
  • Well, I am not completely sure, yet I suggest, if someone ever face this problem, to add a new tomcat server in eclipse "Servers" view, while removing the previous one. It seems to me that it could be the reason, yet I am not sure. – Andy Silver Sep 01 '15 at 21:23

0 Answers0