0

Four days ago, I started getting this error message every time I tried to connect to my DB:

java.lang.NoClassDefFoundError: javax/persistence/spi/PersistenceProvider

I got it after some time that I was working on other parts of my app, not including DB access, so I don't really know what caused this error. All I've done in the past four days, was to try googling different combinations of the error above... So after all that googling, I figured out that, although the error makes it look like I have a missing jar, that is not the case. (I have javax.persistence.jar and javax.persistence-2.1.0-rc1.jar in my WebContent/WEB-INF/lib and the Web App Libraries is included in the build path). The only other thing I thought of is maybe I have some version contradiction, and that's why javax.persistence.spi.PersistenceProvider can not be built, but I have no clue on to how to check that. And there's also the possibility that I'm mistaken and that there's another problem....

I'm adding here my /WebContent/WEB-INF/web.xml and /src/main/resources/META-INF/persistence.xml. If there is anything else needed, please let me know and I'll add it. I've been trying to solve this problem for already 4 days, and I am really fustrated, please help!

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         id="WebApp_ID" 
         version="3.1">
  <display-name>InaalEchtok</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="model_persistence_unit">
        <description>Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!-- <class></class> -->
        <properties>
            <!-- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <!-- <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/fdb" /> -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/fdb?createDatabaseIfNotExist=true" />
            <property name="javax.persistence.jdbc.user" value="****" />
            <property name="javax.persistence.jdbc.password" value="*********" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
        </properties>
    </persistence-unit>
</persistence>

These are my referenced libraries:

referenced libraries

and this is my WEB-INF/lib:

WEB-INF/lib

Ma'or
  • 95
  • 1
  • 2
  • 11
  • 2
    Why have two versions of the javax.persistence jar? Try removing one. – ProgrammersBlock Aug 21 '17 at 12:34
  • If removing one version doesn't work, I have seen this error when a class can't be initialized, and from then on it results in NoClassDefFoundError. If the initialization can be resolved and succeed, the error will also be resolved. – Vivek Chavda Aug 21 '17 at 13:22
  • @ProgrammersBlock you mean `javax.persistence.jar` and `javax.persistence-2.1.0-rc1.jar`? – Ma'or Aug 21 '17 at 13:23
  • From what I read, if there are two versions of the same jar, the compiler will just pick one of them, so that's not suppose to be a problem. Also, when I try to erase one, I get an error. – Ma'or Aug 21 '17 at 13:55
  • Not exactly. If you have two versions of the same jar, the ClassLoader will pickup classes from the first jar it finds the class in. Therefore, it could, in certain circumstances, be picking up classes from both jars and those versions might not play well together. The ClassLoader does not decide which jars are valid and which jars are not. – ProgrammersBlock Aug 22 '17 at 02:17
  • @ProgrammersBlock , I removed one of them, and still get the same error. – Ma'or Aug 23 '17 at 11:24

1 Answers1

0

Hip hip hooray!!! Well, this isn't exactly an answer, but this is how I solved my error:

I converted my project to a Maven project and tried running it. Now the error message was more specific: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql. So I added mysql-connector-java-5.1.43-bin.jar to my WEB-INF/lib folder and now it works!!!

Ma'or
  • 95
  • 1
  • 2
  • 11