0

Why doesn't sbt automatically add libraryDependencies to class path? I am trying to add jdbc-sqlite to my project however it can't find the driver. The lib dependency is being managed by sbt so it should be part of the class path. But I guess not, so how can I add it?

It feels like bad practice to have defined path references to these libraries that only exist on my box.

name := "CacheWarmer"

version := "0.1"

scalaVersion := "2.12.3"

mainClass in Compile := Some("process.Daemon")

libraryDependencies ++= Seq(
  "org.xerial" % "sqlite-jdbc" % "3.20.0" % "test"
)

package process

import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement

Code

object Daemon {
  def main(args: Array[String]): Unit = {
    //Gets java.sql.SQLException: No suitable driver found for jdbc:sqlite::memory:
    val connection:Connection = DriverManager.getConnection("jdbc:sqlite::memory:")
  }
}
2
  • Of course compile/runtime dependencies are available in the sbt run & test classpath.
    – cchantep
    Sep 1, 2017 at 18:26
  • Not according to my run time which says it can't find my jdbc-sqlite driver Sep 1, 2017 at 18:31

2 Answers 2

2

Maven central tells that to use that dependency in SBT you should use:

libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.20.0" --> Notice there is no double %

The %% tells sbt to append the current scala version to the artifact name. Assuming you are running scala 2.11:

libraryDependencies += "org.some" %% "myscala" % "3.20.0"

gets desuggered to:

`libraryDependencies += "org.some" % "myscala_2.11" % "3.20.0"`
2

You have put the sqlite-jdbc dependency into the test scope. Naturally, such dependencies are only available in the test classpath, but not in the "main" classpath. Usually you use the test scope for test dependencies, e.g. test libraries like Scalatest or JUnit.

In order for a library to be available in your "main" classpath, you have to use the compile scope, or, equivalently, not using a scope classifier at all:

libraryDependencies ++= Seq(
  "org.xerial" % "sqlite-jdbc" % "3.20.0"
)
2
  • 1
    They don't have a scala version tacked on which results in the issue no suitable scala version found. Sep 1, 2017 at 18:37
  • @Dr.Knowitall sorry, my bad, should've removed the %% in favor of %. Since this is a pure Java library, there is no need to append Scala binary version to it, which is exactly what %% does. Fixed the answer. Sep 3, 2017 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.