2
時拋出的RuntimeException當我運行命令:爲什麼它運行的應用程序
d:\ scalaTest> FSC Summer.scala ChecksumAccumulator.scala
d:\ scalaTest>斯卡拉大的hello world
的例外是:
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Summer
at scala.sys.package$.error(package.scala:27)
at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
at scala.Option.getOrElse(Option.scala:108)
at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
這是在源代碼中cludeing 2個階文件:
一個是Summer.scala:
import ChecksumAccumulator.calculate
object Summer {
def main(args:Array[String]) {
for(arg <- args)
println(arg + ":" + calculate(arg))
}
}
另一個是ChecksumAccumulator.scala:
import scala.collection.mutable.Map
class ChecksumAccumulator {
private var sum=0
def add(b:Byte) { sum += b }
def checksum():Int = ~(sum & 0xFF) + 1
}
object ChecksumAccumulator {
private val cache = Map[String,Int]()
def calculate(s:String):Int =
if(cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
for(c<-s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s->cs)
cs
}
}
非常感謝
順便說一句:這是Martin Odersky編寫的書「Scala 2nd.Edition編程」中的例子
良好的工作運行! 事實是,我只是設置路徑,並不設置類路徑與斯卡拉 – jimmy