2

I'm trying to get into the habit of using build tools. However, sbt is giving me some trouble, and I don't yet know enough about it to troubleshoot the cause of the problem.

I'm trying to add JotaTime as a dependency to my project.

My build.sbt in full looks like this:

name := "MyProject"

libraryDependencies += "joda-time" % "joda-time" % "2.3"

Now, it seems to find the library and finish updating without issue. I can start using joda-time in my IDE. I can import the modules, autocomplete works, etc.. However, once I try to actually run the project I get these errors:

Error:scalac: error while loading LocalDateTime, class file 'C:\Users\myname\.ivy2\cache\joda-time\joda-time\jars\joda-time-2.3.jar(org/joda/time/LocalDateTime.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 10 at byte 42)

The exact code in question is this:

def main(args: List[String]) = {
    println(new LocalDateTime(System.currentTimeMillis))
}

To try and troubleshoot this a bit. I removed the dependency line from the build.sbt and instead downloaded and added the jota-time library manually via my IDE (version 2.3, just as in the build file), and everything works A-OK. No errors. Everything compiles and runs.

What would be causing the version of JodaTime that sbt downloads to be broken?

user3308774
  • 1,354
  • 4
  • 16
  • 20
  • possible duplicate of [Class broken error with Joda Time using Scala](http://stackoverflow.com/questions/13856266/class-broken-error-with-joda-time-using-scala) – lpiepiora Jul 06 '14 at 17:39

1 Answers1

3

Add this to your build.sbt:

libraryDependencies += "org.joda" % "joda-convert" % "1.6"

Or write this:

libraryDependencies ++= Seq( "joda-time" % "joda-time"    % "2.3"
                           , "org.joda"  % "joda-convert" % "1.6"
                           )

Whichever style is up to you, but that should fix it.

Joda-Time requires Joda-Convert to work in Scala, don't ask me why, it just does.

Look [here] for more info

Community
  • 1
  • 1
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
  • The same applies when using the Maven Scala Plugin by the way. – Angelo Genovese Jul 06 '14 at 19:28
  • 1
    You can find explanation in [joda-time](http://www.joda.org/joda-time/upgradeto230.html) documentation `Joda-Time uses annotations from Joda-Convert. In the Java programming language, this dependency is optional, however in Scala it is not. Scala users must manually add the Joda-Convert v1.2 dependency.` – adjablon Jul 06 '14 at 20:14
  • @adjablon that's the explanation I forgot about – Electric Coffee Jul 06 '14 at 21:29
  • As a recommendation, instead of using JodaTime directly try to use nscala-time (https://github.com/nscala-time/nscala-time). It's a very nice JodaTime wrapper for Scala. – Mario Camou Jul 07 '14 at 09:30