Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I use a Java lib from the Scala REPL but not from a script?

I’m working on a Scala script which uses Joda Time. Up until today, this was working fine. Somehow, something’s changed and it’s no longer working.

This works:

$ scala -cp "lib/*"
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.joda.time._
import org.joda.time._

scala> Period.minutes(5)
res0: org.joda.time.Period = PT5M

but this doesn’t:

$ scala -cp "lib/*" test.scala
/Users/avi/Dev/experiments/rollups/scala/test.scala:4: error: object joda is not a member of package org
import org.joda.time._
           ^
one error found

test.scala contains only:

#!/usr/bin/env scala -cp lib/* -deprecation
!#

import org.joda.time._

Period.minutes(5)

this also doesn’t work:

$ scala -cp "lib/*" -e "import org.joda.time._"
/var/folders/c4/gh5y9_cx5bz8x_4wm060l_mm0000gn/T/scalacmd1248995773392653303.scala:1: error: object joda is not a member of package org
import org.joda.time._
           ^
one error found

It’s also not caused by using * in the cp arg:

$ scala -cp lib/joda-time-2.0.jar:lib/joda-convert-1.2.jar -e "import org.joda.time._"
/var/folders/c4/gh5y9_cx5bz8x_4wm060l_mm0000gn/T/scalacmd5438658792813459030.scala:1: error: object joda is not a member of package org
import org.joda.time._
           ^
one error found

…It’s just so crazy because this was working last time I worked on this project, just a day or two ago! And now it’s not working, and I guess I must have changed something, but honestly I can’t think of what it might be.

Help!

like image 847
Avi Flax Avatar asked Jan 01 '12 03:01

Avi Flax


1 Answers

TL;DR: fsc, the “fast compilation daemon”, had a problem with its cache; fsc -shutdown resolved the problem.

Seth Tisue in the Scala IRC channel on FreeNode was able to help me solve my problem — it had something do with with fsc the “fast offline compiler daemon”. When the scala command is used to run a script, it uses fsc, and it appears that somehow the classpath used/cached by the daemon got messed up.

It turns out there are a few ways to work around this:

  • pass the arg -nocompdaemon to scala to just not use fsc at all
    • works, and should be failproof, but slow
  • run fsc -shutdown
    • the daemon will be automatically restarted the next time you use scala
  • run fsc -reset to reset the daemon’s caches
    • would probably be faster than shutting it down but the least failproof option

I still don’t know exactly what caused this problem to occur in the first place, but the impression I got from Seth and from the fsc page is that this sort of things just happens sometimes.

Thanks, Seth!

like image 170
Avi Flax Avatar answered Oct 28 '22 15:10

Avi Flax