从单个 jar 运行时,找不到适用于 jdbc:mysql 的驱动程序
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 sbt-assemble 插件“uberjar”我的应用程序时遇到了这个问题。就我而言,问题在于在组装过程中丢失
META-INF/services/java.sql.Driver
文件。所以我需要更改程序集配置以使文件保持在原位: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:能给个初始化代码的例子吗?
JDBC驱动程序通常需要加载驱动程序的类来调用静态初始化代码并将其注册到DriverManager中。对于MySQL,这可以通过以下方式完成:
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: