正在追加列表[数据库]
我是 Scala 新手,正在编写一个简单的 rss 阅读器。 我有类管理器来管理提要和内容。
package lib
import scala.xml._
import java.net.URL
import net.liftweb.couchdb.{CouchDB, Database}
import dispatch.{Http, StatusCode}
/**
* @author smix
*
* Feeds manager
*/
object Manager {
var db = List[Database]()
/*
* Initialize CouchDb databases
*/
def init = {
this.appendDb(new Database("myserver.com", 5984, "content"))
}
/*
* Append a new database to the databases list
*/
private def appendDb(database: Database) : Unit = {
database :: db
// Strange exception if database has been already created
/* try {
this.db.head.createIfNotCreated(new Http())
} catch {
case e:java.lang.NoClassDefFoundError => {}
} */
}
/*
* Fetch articles from feed by url
*/
def fetchItems(feedUrl: String): List[scala.xml.Elem] = {
val rssFeed = XML.load( (new URL(feedUrl)).openConnection.getInputStream )
val items = rssFeed \ "channel" \ "item"
val articles: List[scala.xml.Elem] = List()
for(item <- items) {
item :: articles
}
articles
}
}
我想将内容存储在 CouchDb 中。我需要有沙发数据库列表(提要、文章等)。我写了类,但是当我调用appendDb时,出现错误:
Exception in thread "main" java.lang.NoClassDefFoundError: lib/Manager$
at collector$.main(collector.scala:5)
at collector.main(collector.scala)
Caused by: java.lang.ClassNotFoundException: lib.Manager$
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more
当我重写db
定义时:var db = List[Int]()
和appendDb的第一行:< code>1 :: this.db 项目运行良好...奇怪。
另外,有趣的是,为什么当我为现有数据库调用 createIfNotCreated 时会出现异常(appendDb 中注释的 try-catch 块)。
I am new with Scala and I am writing a simple rss reader.
I have class Manager for managing feeds and content.
package lib
import scala.xml._
import java.net.URL
import net.liftweb.couchdb.{CouchDB, Database}
import dispatch.{Http, StatusCode}
/**
* @author smix
*
* Feeds manager
*/
object Manager {
var db = List[Database]()
/*
* Initialize CouchDb databases
*/
def init = {
this.appendDb(new Database("myserver.com", 5984, "content"))
}
/*
* Append a new database to the databases list
*/
private def appendDb(database: Database) : Unit = {
database :: db
// Strange exception if database has been already created
/* try {
this.db.head.createIfNotCreated(new Http())
} catch {
case e:java.lang.NoClassDefFoundError => {}
} */
}
/*
* Fetch articles from feed by url
*/
def fetchItems(feedUrl: String): List[scala.xml.Elem] = {
val rssFeed = XML.load( (new URL(feedUrl)).openConnection.getInputStream )
val items = rssFeed \ "channel" \ "item"
val articles: List[scala.xml.Elem] = List()
for(item <- items) {
item :: articles
}
articles
}
}
I want to store content in CouchDb. I need to have list of couch databases(feeds, articles, etc...). I wrote class but when I call appendDb i get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: lib/Manager$
at collector$.main(collector.scala:5)
at collector.main(collector.scala)
Caused by: java.lang.ClassNotFoundException: lib.Manager$
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more
When I rewrited db
definition: var db = List[Int]()
and the first line of appendDb: 1 :: this.db
project ran fine... Strange.
Also, it is interesting why am I getting exception when I call createIfNotCreated for existing database(commented try-catch block in appendDb).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常表明您在运行程序时丢失了一些类(可能是一个或多个 JAR 文件),尽管它们要么与编译程序无关,要么可用。
您还应该注意
appendDb
中的第一行没有完成任何操作。它通过将database
consingdatabase
到db
引用的List
的前面来构建一个新的List
,但生成的结果值被丢弃。也许你的意思是这样的:The exception indicates that you're missing some classes (one or more JAR files, presumably) when you run your program, though they're either irrelevant to compiling it or they are available then.
You should also note that the first line in
appendDb
accomplishes nothing. It builds a newList
by consingdatabase
onto the front of theList
referred to bydb
, but the resulting value is discarded. Perhaps you meant this: