从单个 jar 运行时,找不到适用于 jdbc:mysql 的驱动程序

发布于 2024-11-19 03:49:42 字数 564 浏览 3 评论 0原文

我有一个 Scala 测试项目,它将一些信息写入 mysql 数据库。我使用 sbt 设置项目,并使用 sbt-eclipsify,这样我就可以从 eclipse 运行它。我还使用 sbt 插件 sbt-assemble 创建一个包含依赖 jar 中我需要的所有类的单个 jar。我可以从 eclipse 和 sbt 运行该程序,没有任何问题。

我从 sbt-assemble 构建的单个 jar 运行它:

java -classpath target/test1-assembly-1.0-SNAPSHOT.jar example.InsertDataIntoDatabase

但它失败了:

没有找到适合jdbc的驱动:mysql://localhost:3306/test

我的第一个想法是 sbt-assemble 可能错过了 mysql 驱动程序依赖项,但我解压了 jar,并在里面找到了 com/mysql/jdbc/Driver.class它。

是否还有其他依赖项可能会丢失?

如何解决这个问题?

I have a Scala test project which writes some information to a mysql database. I set up the project with sbt, and use sbt-eclipsify so I can run it from eclipse. I also used the sbt plugin sbt-assembly to create a single jar with all the classes I need from the dependent jars. I can run the program with no problem from eclipse and from sbt.

I run it from the single jar that sbt-assembly builds:

java -classpath target/test1-assembly-1.0-SNAPSHOT.jar example.InsertDataIntoDatabase

but it fails with:

No suitable driver found for jdbc:mysql://localhost:3306/test

My first thought was that sbt-assembly might have missed the mysql driver dependency, but I unzipped the jar, and found com/mysql/jdbc/Driver.class inside it.

Is there some other dependency it could be missing?

How can this be solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-11-26 03:49:42

我在使用 sbt-assemble 插件“uberjar”我的应用程序时遇到了这个问题。就我而言,问题在于在组装过程中丢失 META-INF/services/java.sql.Driver 文件。所以我需要更改程序集配置以使文件保持在原位:

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs@_*) =>
    xs.map(_.toLowerCase) match {
      case ("manifest.mf" :: Nil) |
           ("index.list" :: Nil) |
           ("dependencies" :: Nil) |
           ("license" :: Nil) |
           ("notice" :: Nil) => MergeStrategy.discard
      case _ => MergeStrategy.first // was 'discard' previousely
    }
  case "reference.conf" => MergeStrategy.concat
  case _ => MergeStrategy.first
}

I encountered this issue when using sbt-assembly plugin to "uberjar" my app. In my case the problem was in loosing META-INF/services/java.sql.Driver file during assembly process. So I needed to change assembly configuration to make the file stay in place:

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs@_*) =>
    xs.map(_.toLowerCase) match {
      case ("manifest.mf" :: Nil) |
           ("index.list" :: Nil) |
           ("dependencies" :: Nil) |
           ("license" :: Nil) |
           ("notice" :: Nil) => MergeStrategy.discard
      case _ => MergeStrategy.first // was 'discard' previousely
    }
  case "reference.conf" => MergeStrategy.concat
  case _ => MergeStrategy.first
}
可可 2024-11-26 03:49:42

能给个初始化代码的例子吗?

JDBC驱动程序通常需要加载驱动程序的类来调用静态初始化代码并将其注册到DriverManager中。对于MySQL,这可以通过以下方式完成:

Class.forName("com.mysql.jdbc.Driver")

Could you give an example of initialization code?

JDBC drivers usually require loading driver's class to call static initialization code and register it in the DriverManager. For MySQL this could be done with:

Class.forName("com.mysql.jdbc.Driver")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文