I'm trying to run some scala codes from the book "Scala in Action". Here are the codes:
MongoClient.scala:
package com.scalainaction.mongo
import com.mongodb._
class MongoClient(val host: String, val port: Int){
require(host != null, "You have to provide a host name")
private val underlying = new Mongo(host, port)
def this() = this("127.0.0.1", 27017)
def version = underlying.getVersion
def dropDB(name: String) = underlying.dropDatabase(name)
def createDB(name: String) = DB(underlying.getDB(name))
def db(name: String) = DB(underlying.getDB(name))
}
DB.scala:
package com.scalainaction.mongo
import com.mongodb.{DB => MongoDB}
import scala.collection.convert.Wrappers._
class DB private(val underlying: MongoDB) {
def collectionName = for(name <- new
JSetWrapper(underlying.getCollectionNames)) yield name
}
object DB {
def apply(underlying: MongoDB) = new DB(underlying)
}
test.scala:
package com.scalainaction.mongo
import com.scalainaction.mongo._
class test{
def client = new MongoClient
def db = client.createDB("mydb")
for (name <- db.collectionName) println(name)
}
After compiling using
scalac -cp /.../mongo-java-driver-2.11.3.jar MongoClient.scala DB.scala test.scala
I got:
MongoClient.scala:7: warning: constructor Mongo in class Mongo is deprecated: see corresponding Javadoc for more information.
private val underlying = new Mongo(host, port)
^
one warning found
It seems OK(?). But after invoking
scala -cp /.../mongo-java-driver-2.11.3.jar -deprecation test.scala
it showed:
/.../test.scala:1: error: illegal start of definition
package com.scalainaction.mongo
^
one error found
How can I run test.scala without error? Thanks a lot for your help!