正在追加列表[数据库]

发布于 2024-09-11 23:40:28 字数 1984 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-09-18 23:40:29

该异常表明您在运行程序时丢失了一些类(可能是一个或多个 JAR 文件),尽管它们要么与编译程序无关,要么可用。

您还应该注意 appendDb 中的第一行没有完成任何操作。它通过将 database consing databasedb 引用的 List 的前面来构建一个新的 List,但生成的结果值被丢弃。也许你的意思是这样的:

  db = database :: db

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 new List by consing database onto the front of the List referred to by db, but the resulting value is discarded. Perhaps you meant this:

  db = database :: db
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文