0

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?

2
  • That's not a compiler error, and you didn't show how you're trying to launch the application. Commented Nov 2, 2021 at 2:28
  • You're right @chrylis-cautiouslyoptimistic- I updated my question, sorry I am new to Java. I am just trying to run it in Eclipse. I did check the run config and it shows maven dependencies. I'm not sure if I might be missing a step there. Commented Nov 2, 2021 at 2:37

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the detailed explanation. It really helped me out! I accepted your answer.
0

I just figured it out.

When I imported ormlite-core and ormlite-jdbc, I imported different versions of them. I changed it in my maven POM to where it is the same version and that corrected the issue!

This is what I changed my maven POM dependencies to:

<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>5.6</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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.