Home > Software design >  NoClassDefFoundError: java/sql/SQLException error postgres hibernate
NoClassDefFoundError: java/sql/SQLException error postgres hibernate

Time:08-06

I am working on a project using JavaFX with a PostgreSQL database and Hibernate. For some reason, I get this exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
    ... 2 more
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 28 more

Here is my persistence.xml file:

<persistence version="2.0"
             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">

    <!-- Define a name used to get an entity manager. Define that you will
    complete transactions with the DB  -->
    <persistence-unit name="universalapp" transaction-type="RESOURCE_LOCAL">

        <!-- Define the object that should be persisted in the database -->
        <class>com.ta.universalapp.Venit</class>
        <properties>
            <!-- Driver for DB database -->
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <!-- URL for DB -->
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/universal_app" />
            <!-- Username -->
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <!-- Password -->
            <property name="javax.persistence.jdbc.password" value="password" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
</persistence>

And this is my main function:

private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence
        .createEntityManagerFactory("universalapp");

public static void main(String[] args) {
    launch();
    getIncome(9);
    getIncomes();
    changeIncome(16, "test_new_database");
    deleteIncome(27);
    ENTITY_MANAGER_FACTORY.close();
}

I don't know what I am doing wrong.

CodePudding user response:

Looks like your JavaFX application runs in module mode, but you don't configure that you application needs the java.sql module. Add --add-modules java.sql to your command line flags. Also see why is java/sql module not resolved by default from an automatic module and are there other system modules which are not resolved by default

  • Related