运行 程序使用 OrmLite 时尝试解决 ClassNotFound 错误

Trying to resolve ClassNotFound error when running program using OrmLite

我正在尝试使用 OrmLite 连接到 SQLite 数据库(不是 android)。我已经阅读了文档,我相信我的代码是正确的,但是我在尝试 运行 时遇到 运行 时间错误。我正在使用 Maven 导入依赖项。

这是我的代码:

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);
        ...
    }
}

这是我的 maven POM 文件的依赖部分:

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

我正在尝试 运行 Eclipse 中的程序。我检查了 运行 配置,它在“依赖项”选项卡的类路径中显示了 Maven 依赖项。这是我在 Eclipse 中 运行ning 时遇到的错误:

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

有什么想法吗?

我刚刚弄明白了。

当我导入 ormlite-core 和 ormlite-jdbc 时,我导入了它们的不同版本。我在我的 maven POM 中将它更改为相同版本并纠正了问题!

这就是我将 maven POM 依赖项更改为:

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

Exception in thread "main" java.lang.NoClassDefFoundError 通常发生在您有 Class A 试图访问 Class BClass Bclasspath 中不可用时。简单地说,在 Class A 你有 :

import com.company.ClassB;

class A {

}

然后在Class Bjar中,Class B那里不再可用。

你的情况是ormlite-coreormlite-jdbc之间的版本不兼容。 ormlite-jdbc 中的 Class SqlServerDatabaseType 正在寻找 ormlite-core 中的 class BooleanNumberFieldConverter,它在 4.48 版本中不再可用ormlite-core.

要解决您的问题,您必须将 ormlite-core 的版本更改为 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>