2

当我运行命令时:

D:\scalaTest>fsc Summer.scala ChecksumAccumulator.scala

D:\scalaTest>scala 夏天你好世界

例外是:

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)

这是包含两个 scala 文件的源代码:

一个是 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 编程”一书中的示例

4

1 回答 1

4

如果我不得不猜测,那是因为您的CLASSPATH环境变量不包含..

只要您的Summer.class文件生成正确,您应该能够让它运行

scala -cp . Summer hello world
于 2012-05-05T01:44:48.923 回答