简体   繁体   English

在应用图像上找不到适用于 jdbc:sqlite 的驱动程序

[英]No suitable driver found for jdbc:sqlite on app image

I have a JavaFX app that connects to SQLite using我有一个 JavaFX 应用程序连接到 SQLite 使用

Connection connection = DriverManager.getConnection("jdbc:sqlite:data.sqlite");

I have the dependency for SQLite driver on my pom file:我的 pom 文件中有 SQLite 驱动程序的依赖项:

<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.32.3.2</version>
</dependency>

I can run the App smoothly using Maven Goals javafx:compile and javafx:run , everything works as expected and the app has no trouble querying the db.我可以使用 Maven 目标javafx:compilejavafx:run顺利运行应用程序,一切正常,应用程序查询数据库没有问题。 The data.sqlite file is created on the project's root folder (same folder as src/ and pom.xml ).在项目的根文件夹(与src/pom.xml相同的文件夹)上创建data.sqlite文件。

However, when I try to run an executable file built with jlink , I get the error described on the thread's title.但是,当我尝试运行使用jlink构建的可执行文件时,我收到了线程标题中描述的错误。 First, I ran maven goal javafx:jlink .首先,我跑了 maven 目标javafx:jlink This generates some stuff at target/image/ .这会在target/image/处生成一些东西。 I navigated to target/image/bin and ran ./java -m <name-of-my-module>/<group-id>.App , which is the command for running the file I built.我导航到target/image/bin并运行./java -m <name-of-my-module>/<group-id>.App ,这是运行我构建的文件的命令。 Then I get:然后我得到:

No suitable driver found for jdbc:sqlite:data.sqlite

And a NullPointerException because of the database init routines that try to access my database connection.还有一个NullPointerException ,因为数据库初始化例程尝试访问我的数据库连接。 How can I fix this?我怎样才能解决这个问题? Thanks in advance.提前致谢。

EDIT: It seems that the solution has something to do with adding a Class.forName() statement before calling DriverManager.getConnection() .编辑:似乎该解决方案与在调用DriverManager.getConnection()之前添加Class.forName()语句有关。 I tried doing the following:我尝试执行以下操作:

Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:data.sqlite");

By doing that, now the app renders, However.通过这样做,现在应用程序呈现,但是。 it still crashes.它仍然崩溃。 I get ClassNotFoundException: org.sqlite.JDBC .我得到ClassNotFoundException: org.sqlite.JDBC Any hints...?任何提示...?

I fear this will not work directly with jlink because as far as I have seen SQLite is not yet modularized.我担心这不会直接与 jlink 一起使用,因为据我所知 SQLite 尚未模块化。 I have described an alternative approach here https://github.com/dlemmermann/JPackageScriptFX which also uses jlink internally but only to create a dedicated runtime.我在这里描述了另一种方法https://github.com/dlemmermann/JPackageScriptFX它也在内部使用 jlink 但仅用于创建专用运行时。 With this approach linking SQLite works.使用这种方法链接 SQLite 可以工作。 I know that because my own applications also use the exact same version of the SQLite driver without problems.我知道这是因为我自己的应用程序也使用完全相同版本的 SQLite 驱动程序而没有问题。

I was able to solve this by using Draque Thompson's Module Info Inject .我能够通过使用 Draque Thompson 的Module Info Inject来解决这个问题。 Basically what the program does is creating a module-info.class for your .jar , letting JLink see it as a module.基本上,该程序所做的是为您的 .jar 创建一个module-info.class .jar让 JLink 将其视为一个模块。 (Sorry if the explanation is lacking, I don't have deep knowledge of Java's module system). (对不起,如果缺乏解释,我对Java的模块系统没有深入的了解)。

So basically what I done was:所以基本上我所做的是:

  1. Clone Module-Info-Inject and run it.克隆 Module-Info-Inject 并运行它。
  2. Select the desired jar folder and click "Inject". Select 所需的 jar 文件夹并单击“注入”。
  3. On the generated module-info.class file, get the module's name.在生成的module-info.class文件中,获取模块的名称。 In my case it was sqlite.jdbc .就我而言,它是sqlite.jdbc
  4. Add the new module to my project's module-info.java .将新模块添加到我的项目的module-info.java中。

The generated module-info.class file for SQLite driver:为 SQLite 驱动程序生成的module-info.class文件:

module sqlite.jdbc {
    requires transitive java.logging;
    requires transitive java.sql;

    exports org.sqlite;
    exports org.sqlite.core;
    exports org.sqlite.date;
    exports org.sqlite.javax;
    exports org.sqlite.jdbc3;
    exports org.sqlite.jdbc4;
    exports org.sqlite.util;

    provides java.sql.Driver with org.sqlite.JDBC;
}

Requiring the module in my project's module-info.java :在我的项目的module-info.java中需要模块:

module com.demo {
    requires sqlite.jdbc;

    / * ... * /

    exports pkg.app;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM