I am working through the examples in ScalaInAction and I am having trouble running examples which spread across multiple files and a placed in a single package.
Here is the code.
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")
val underlying = new com.scalainaction.mongo.Mongo
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) {
private def collection(name: String) = underlying.getCollection(name)
def readOnlyCollection(name: String) = new DBCollection(collection(name))
def administrableCollection(name: String) = new
DBCollection(collection(name)) with Administrable
def updatableCollection(name: String) = new
DBCollection(collection(name)) with Updatable
def collectionNames = for(name
DBCollection.scala
package com.scalainaction.mongo
import com.mongodb.{DBCollection => MongoDBCollection }
import com.mongodb.DBObject
class DBCollection(override val underlying: MongoDBCollection) extends ReadOnly
trait ReadOnly {
val underlying: MongoDBCollection
def name = underlying.getName
def fullName = underlying.getFullName
def find(doc: DBObject) = underlying.find(doc)
def findOne(doc: DBObject) = underlying.findOne(doc)
def findOne = underlying.findOne
def getCount(doc: DBObject) = underlying.getCount(doc)
}
trait Administrable extends ReadOnly {
def drop: Unit = underlying.drop
def dropIndexes: Unit = underlying.dropIndexes
}
trait Updatable extends ReadOnly {
def -=(doc: DBObject): Unit = underlying.remove(doc)
def +=(doc: DBObject): Unit = underlying.save(doc)
}
All the programs are placed inside the same package
com.scalainaction.mongo
.
I don't use an IDE so I compile these files by running
scalac mongoclient.scala DB.scala DBCollection.scala
My $CLASSPATH includes the mongodb.jar file and also points to com.scalainaction.mongo folder in my application directory
And now I intend to run a program which uses the package by running scala quickTour.scala -cp $CLASSPATH`
import com.scalainaction.mongo._ import com.mongodb.BasicDBObject def client = new MongoClient("127.0.0.1", 27017) def db = client.db("mydb") for(name
But my application is unable to find the MongoClient class and I get this error
quickTour.scala:1: error: object scalainaction is not a member of package com import com.scalainaction.mongo._ ^ /Users/sid/scala_apps/quickTour.scala:4: error: MongoClient does not have a constructor def client = new MongoClient("127.0.0.1", 27017) ^ two errors found
I don't follow why it doesn't find the constructor. I have defined an overloaded constructor with the def this method
Why can't it find com.scalanation.mongo?
I would appreciate any help on this
** UPDATE **
Files in my com/scalainaction/mongo folder are
Administrable$class.class DB$.class ReadOnly$class.class Administrable.class DB.class ReadOnly.class DB$$anon$1.class DBCollection.class Updatable$class.class DB$$anon$2.class MongoClient$$anonfun$1.class Updatable.class DB$$anonfun$collectionNames$1.class MongoClient.class
The problem is that your folder com and/or com/scalainaction is empty. Put some classes there and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With