0

I created a simple project.

"sbt run" works fine.

After I run "sbt package" jar file appears, but it does not work.

$ scala target/scala-2.9.2/hello_2.9.2-0.1.jar

The following error occures:

java.lang.NoClassDefFoundError: com/mongodb/DBObject

Here are my files:

Hello.scala

package greeter
object Hello extends App {
    import com.mongodb.casbah.Imports._
    val mongoClient =  MongoClient()
    println("Connected to MongoDB")
    val collection = mongoClient("test")("foo")
    val doc = MongoDBObject("msg" -> "Hi from Scala!")
    collection.insert(doc)
    println("Doc inserted");
}

build.sbt

name := "hello"

mainClass := Some("greeter.Hello")

version := "0.1"

scalaVersion := "2.9.2"

libraryDependencies += "org.mongodb" % "casbah_2.9.2" % "2.5.0"

resolvers += "snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

resolvers += "releases"  at "https://oss.sonatype.org/content/groups/scala-tools"

1 Answer 1

1

You need to add the library dependencies to your classpath when running the scala command. Assuming you download all the libraries jars in a lib folder, you will need to run:

scala -cp "lib/*" target/scala-2.9.2/hello_2.9.2-0.1.jar

In build.sbt, use the following setting to automatically download all the jars in a lib_managed folder:

retrieveManaged := true
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. So I need to copy all jar files from lib_managed folder to lib folder? Is there a simple command for that, or do I need to use some tricky bash command? And how can I combine all those jars in one?
Actually you can just use the lib_managed folder directly: scala -cp "lib_managed/*" target/scala-2.9.2/hello_2.9.2-0.1.jar Or you can copy the whole folder with: cp -rf lib_managed lib
I tried -cp "lib_managed/*", but it does not work. The same error occures java.lang.NoClassDefFoundError: com/mongodb/DBObject. Than I copied all jars to lib filder and used -cp "lib/*", and it works.
Sorry, I forgot that the jars are not at the root of lib_managed. The best way to make a clean lib dir with all the jars is to create an empty lib dir: mkdir lib and then copy the jars: find lib_managed/ -name *.jar -exec cp {} lib/ \;

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.