Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable driver found for jdbc:mysql when running from single jar

Tags:

mysql

scala

sbt

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?

like image 256
Willard Avatar asked Nov 04 '22 18:11

Willard


1 Answers

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
}
like image 56
Tvaroh Avatar answered Nov 14 '22 23:11

Tvaroh