要导入的内容<为斯卡拉的jodatime工作

时间:2010-11-19 14:58:39

标签: scala jodatime

我想比较LocalDateTime但最终会出现异常。

我有这些课程:

import org.scala_tools.time.Imports._
import org.scala_tools.time.Implicits._

case class Event(id: String, startDate: LocalDateTime, action: String)

case class Events(events: List[Event], timeout: Int) {

   val timeoutDt: LocalDateTime =  new LocalDateTime().minusMinutes(timeout)

   lazy val oldEvents = events.filter { (e: Event) =>
     e.startDate < timeoutDt
   }

   lazy val eventsToSend = events.filterNot { (e: Event) =>
     e.startDate < timeoutDt
   }
}

我遇到了这个例外:

scala> import org.scala_tools.time.Imports._
scala> import org.scala_tools.time.Implicits._

scala> val d1 = new LocalDateTime("2010-11-17T10:10:10") 
d1: org.joda.time.LocalDateTime = 2010-11-17T10:10:10.000

scala> val d2 = new LocalDateTime("2012-11-17T10:10:10")
d2: org.joda.time.LocalDateTime = 2012-11-17T10:10:10.000

scala> val e1 = new Event("a", d1, "a")
scala> val e2 = new Event("b", d2, "b")

scala> val es = Events(List(e1, e2), 10000)

scala> val old = es.oldEvents
java.lang.NoClassDefFoundError: scala/Ordered
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:86)
    at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:51)
    at scala.tools.nsc.util...

我忘了什么?

=======编辑============

我正在使用Scala 2.8.0。感谢@Alex,我发现project/build/Project.scala文件中的依赖项不正确:

import java.io.File
import sbt._

class Project(info: ProjectInfo) extends DefaultProject(info) { 
  // ... omitted code

  // OBS: Must have this to access snapshots
  val snap = ScalaToolsSnapshots

  // Time
  lazy val jodaTime = "joda-time" % "joda-time" % "1.6" 
  lazy val scalaTime = "org.scala-tools" % "time" % "2.8.0-SNAPSHOT-0.2-SNAPSHOT"  // Does not work
}

以下代码有效:

import java.io.File
import sbt._

class Project(info: ProjectInfo) extends DefaultProject(info) { 
  // ... omitted code

  // Time
  lazy val jodaTime = "joda-time" % "joda-time" % "1.6" 
  lazy val scalaTime = "org.scala-tools.time" % "time_2.8.0" % "0.2"
}

仍然不确定为什么快照不起作用。

1 个答案:

答案 0 :(得分:4)

java.lang.NoClassDefFoundError: scala/Ordered表示您有Scala版本不匹配或类路径问题。

您使用的库很可能不是针对您当前的Scala版本编译的。