Search code examples
javatomcat7java-ee-7jpa-2.1hibernate-5.x

How to run app Hibernate 5.x, Jpa 2.1, Java EE7(javaee-api 7.0) on Tomcat7 with Jax-RS(Jersey)


I have a simple application runs succesfully as a Java Application. The frameworks used in app Hibernate 5.x, Jpa 2.1, Java EE7(javaee-api 7.0). And then i wanted to add Jax-Rs support which i want to run it on Tomcat 7.

I got some errors when i want to start application by ServletContextListener. It seems there is no persistence.jar WEB_INF/lib folder but i looked in to folder i has beean created.

java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at com.ulger.app.EntityManagerFactoryMysql.buildEntiyManagerFactory(EntityManagerFactoryMysql.java:18)
    at com.ulger.app.EntityManagerFactoryMysql.<clinit>(EntityManagerFactoryMysql.java:12)
    at com.ulger.app.Server.configure(Server.java:10)
    at com.ulger.app.Server.start(Server.java:6)
    at com.ulger.app.Initializer.contextInitialized(Initializer.java:10)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    ... 14 more

The folder of application in tomcat directory

enter image description here

The deployment descriptor enter image description here

Pom.xml

    <!-- Database Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>
    <!-- Database Connector -->

    <!-- Javax -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <!-- Javax -->

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <!-- Hibernate -->

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- Logging -->

    <!-- JAX-RS (Jersey) -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
    <!-- JAX-RS (Jersey) -->

</dependencies>

Solution

  • Tomcat 7 does not support Java EE 7, and will not load those classes from your WAR. If you reconfigure tomcat to emit debug log output, you should see corresponding warnings about JAR files not being loaded/skipped from your WAR accordingly.

    You can either try to downgrade your dependencies to versions which tomcat 7 does support, or you can try to upgrade tomcat to version 8