I am trying to use OrmLite to connect to a SQLite database (not android). I have read the docs and I believe that my code is correct but I am getting a runtime error when trying to run. I am using Maven to import the dependencies.
Here is my code:
public class AddressBook {
public static void main(String[] args) throws SQLException {
ConnectionSource connectionSource =
new JdbcConnectionSource("jdbc:sqlite:database.db");
Dao<Person, Integer> personDao =
DaoManager.createDao(connectionSource, Person.class);
...
}
}
Here is the dependency section of my maven POM file:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-core -->
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>4.48</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc -->
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>5.6</version>
</dependency>
</dependencies>
I am trying to run the program in Eclipse. I checked the Run Configuration and it shows Maven Dependencies in the classpath on the Dependencies tab. Here is the error I am getting when running in Eclipse:
Exception in thread "main" java.lang.NoClassDefFoundError: com/j256/ormlite/field/converter/BooleanNumberFieldConverter
at com.j256.ormlite.jdbc.db.SqlServerDatabaseType.<clinit>(SqlServerDatabaseType.java:31)
at com.j256.ormlite.jdbc.db.DatabaseTypeUtils.<clinit>(DatabaseTypeUtils.java:31)
at com.j256.ormlite.jdbc.BaseJdbcConnectionSource.initialize(BaseJdbcConnectionSource.java:102)
at com.j256.ormlite.jdbc.JdbcConnectionSource.<init>(JdbcConnectionSource.java:104)
at com.j256.ormlite.jdbc.JdbcConnectionSource.<init>(JdbcConnectionSource.java:47)
at dev.website.addressbook.AddressBook.main(AddressBook.java:19)
Caused by: java.lang.ClassNotFoundException: com.j256.ormlite.field.converter.BooleanNumberFieldConverter
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:520)
... 6 more
Any ideas?
Exception in thread "main" java.lang.NoClassDefFoundError
generally occurs when you have a Class A
trying to access a Class B
and the Class B
is not available in the classpath
. Simply say, in Class A
you have :
import com.company.ClassB;
class A {
}
and then in the jar
of the Class B
, Class B
is no longer available there.
In your case, it is an incompatibility of the version between ormlite-core
and ormlite-jdbc
. The Class SqlServerDatabaseType
in ormlite-jdbc
is looking for the class BooleanNumberFieldConverter
in ormlite-core
which is no longer available in the version 4.48
of ormlite-core
.
To resolve your issue, you have to change the version of ormlite-core
to 5.6
.
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>5.6</version>
</dependency>