gpt4 book ai didi

Scala 初学者在多个文件上编译和运行 scala 程序

转载 作者:行者123 更新时间:2023-12-02 19:55:44 25 4
gpt4 key购买 nike

我正在研究 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


<p><strong>DBCollection.scala</strong></p>

<pre><code>
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)
}
</code>
</pre>

<p>All the programs are placed inside the same package
<code>com.scalainaction.mongo</code>. </p>

<p>I don't use an IDE so I compile these files by running</p>

<pre><code>
scalac mongoclient.scala DB.scala DBCollection.scala
</code>
</pre>

<p>My $CLASSPATH includes the mongodb.jar file and also points to com.scalainaction.mongo folder in my application directory</p>

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.BasicDBObjectdef client = new MongoClient("127.0.0.1", 27017)def db = client.db("mydb")for(name 

但是我的应用程序无法找到 MongoClient 类,并且出现此错误

quickTour.scala:1: error: object scalainaction is not a member of package comimport com.scalainaction.mongo._           ^/Users/sid/scala_apps/quickTour.scala:4: error: MongoClient does not have a constructordef client = new MongoClient("127.0.0.1", 27017)             ^two errors found

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

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

如果有任何帮助,我将不胜感激

** 更新 **

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

Administrable$class.class       DB$.class               ReadOnly$class.classAdministrable.class         DB.class                ReadOnly.classDB$$anon$1.class            DBCollection.class          Updatable$class.classDB$$anon$2.class            MongoClient$$anonfun$1.class        Updatable.classDB$$anonfun$collectionNames$1.class MongoClient.class

最佳答案

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

关于Scala 初学者在多个文件上编译和运行 scala 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18276940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com