为什么我可以使用Scala REPL中的Java库而不使用脚本中的Java库? [英] Why can I use a Java lib from the Scala REPL but not from a script?

查看:60
本文介绍了为什么我可以使用Scala REPL中的Java库而不使用脚本中的Java库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用Joda Time的Scala脚本。到今天为止,一切正常。

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.

这有效:

$ 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

但这不是:

$ 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 仅包含:

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

import org.joda.time._

Period.minutes(5)

这也不起作用:

$ 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

这也不是由于使用<$ c $ cp * > arg:

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.

帮助!

推荐答案

TL; DR: fsc (快速编译守护程序)遇到问题它的缓存; fsc -shutdown 解决了该问题。

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

FreeNode上Scala IRC频道中的Seth Tisue 可以帮助我解决问题,与 fsc 有关快速脱机编译器守护程序。当 scala 命令用于运行脚本时,它使用 fsc ,并且似乎以某种方式使用了类路径/

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:


  • 将arg -nocompdaemon 传递给 scala 只是根本不使用fsc $ b

    • 可以正常工作,并且应该具有防故障功能,但是速度较慢

    • pass the arg -nocompdaemon to scala to just not use fsc at all
      • works, and should be failproof, but slow

      • 下次使用 scala 时,守护程序将自动重新启动

      • the daemon will be automatically restarted the next time you use scala

      • 可能比关闭它要快,但是最不失败的选择

      我仍然不知道到底是什么导致了此问题的发生,但我从塞思和 fsc 页面获得的印象就是这样的事情有时会出现。

      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.

      谢谢塞思!

      这篇关于为什么我可以使用Scala REPL中的Java库而不使用脚本中的Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆