5

我正在研究 ScalaInAction 中的示例,并且在运行分布在多个文件中并放置在单个包中的示例时遇到问题。

这是代码。

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`

导入 com.scalinaction.mongo._
导入 com.mongodb.BasicDBObject

def client = new MongoClient("127.0.0.1", 27017)
def db = client.db("mydb")

对于(名称

但我的应用程序无法找到 MongoClient 类,我收到此错误

quickTour.scala:1: 错误:对象 scalinaction 不是包 com 的成员
导入 com.scalinaction.mongo._
           ^
/Users/sid/scala_apps/quickTour.scala:4:错误:MongoClient 没有构造函数
def client = new MongoClient("127.0.0.1", 27017)
             ^
发现两个错误

我不明白为什么它找不到构造函数。我已经用 def this 方法定义了一个重载的构造函数

为什么找不到 com.scalation.mongo?

我将不胜感激这方面的任何帮助

** 更新 **

我的 com/scalinaction/mongo 文件夹中的文件是

Administrable$class.class DB$.class ReadOnly$class.class
Administrable.class DB.class ReadOnly.class
DB$$anon$1.class DBCollection.class 可更新$class.class
DB$$anon$2.class MongoClient$$anonfun$1.class Updatable.class
DB$$anonfun$collectionNames$1.class MongoClient.class
4

1 回答 1

2

问题是您的文件夹 com 和/或 com/scalinaction 是空的。在那里放一些课程,它应该可以工作。

于 2013-08-16T15:51:21.077 回答