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

Im working on a Scala script that uses Joda Time. Until today, this has worked fine. Somehow, something has changed, and it no longer works.

It 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 it does not:

 $ 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 does not 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 is also not caused by using * in the cp argument:

 $ 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 crazy because it worked the last time I worked on this project, just a day or two ago! And now it does not work, and I think I must have changed something, but to be honest, I can’t think what it could be.

Help!

+6
source share
1 answer

TL DR: fsc , the "fast compilation daemon" had a cache problem; fsc -shutdown resolved the issue.

Seth Tisue on Scala's FreeNode IRC channel was able to help me solve my problem - it had something with fsc "fast compiler daemon". When the scala command is used to run the script, it uses fsc , and it seems that somehow the classpath used / cached by the daemons is confused.

It turns out there are several ways around this:

  • pass arg -nocompdaemon to scala to just not use fsc at all
    • works and should be fault tolerant but slow
  • run fsc -shutdown
    • the daemon will automatically restart the next time you use scala
  • run fsc -reset to reset the daemon cache
    • likely to be faster than disabling it, but the least fault tolerant option

I still don’t know exactly why this problem occurred in the first place, but the impression I got from Seth and from the fsc page is that such things happen sometimes.

Thanks Seth!

+12
source

Source: https://habr.com/ru/post/904826/


All Articles