1

I have the following root project setup:

lazy val root = project.aggregate(rest,backend).dependsOn(rest,backend)                                 
lazy val rest = project
lazy val backend = project.dependsOn(rest).settings(mainClass in (Compile, run) := Some("my.backend.services.Main"))

run in Compile <<= (run in Compile in backend)

When I type run I get an error stating that:

[error] (run-main) java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/db?user=userid&password=password java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/....

So it seems the "mysql" % "mysql-connector-java" % "5.1.26" dependency in the backend project isn't part of the classpath during the run? I find it odd it's just this dependency and not others... Is it the way in which the class is being loaded dynamically?

Is there a work around for this?

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
ThaDon
  • 7,826
  • 9
  • 52
  • 84
  • 2
    Does `fork in run := true` fix it? If so, I'd suspect classloader issues. – Seth Tisue Nov 26 '13 at 14:22
  • Seth, put that in the form of a question so I can accept it! I just had to add that setting like so: `lazy val backend = project.dependsOn(rest).settings(mainClass in (Compile, run) := Some("my.backend.services.Main"), fork in run := true)` – ThaDon Nov 26 '13 at 17:11

1 Answers1

1

Try fork in run := true. If that fixes it, it's probably a classloader issue. (As in, whatever classloader SBT loads the driver in apparently isn't the classloader, or an ancestor of the classloader, that JDBC is using to look up drivers.)

If the fork thing fixes it, you might consider the problem solved. But note that forking runs your program in a freshly spawned JVM, instead of using the JVM you are already running SBT in. Spawning a new JVM takes time.

If you want to attempt a fix that doesn't involve forking, I think that if you include "classloader" in your Stack Overflow and/or Google searches, you'll find lots of relevant information and advice, for example:

lots more out there like these if you look.

Community
  • 1
  • 1
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • I'm almost curious if this is a bug in SBT. I'm sure the driver is being loaded via `Class.forName()` and the appropriate class, although brought in as a dependency, isn't included as a part of the classpath during the run – ThaDon Nov 26 '13 at 20:35
  • @ThaDon: but it isn't that simple, because classloaders. See the links I have just added to my answer – Seth Tisue Nov 26 '13 at 22:15