scala - 从命令行运行时找不到包

时间:2016-06-25 04:40:45

标签: scala

我正在使用this book学习Scala,并且在尝试运行其中一个示例后出现错误。代码:

shapes.scala

package shapes {  
  class Point(val x: Double, val y: Double) {
    override def toString() = "Point(" + x + "," + y + ")"
  }

  abstract class Shape() { 
    def draw(): Unit
  }

  class Circle(val center: Point, val radius: Double) extends Shape {
    def draw() = println("Circle.draw: " + this)
    override def toString() = "Circle(" + center + "," + radius + ")"
  }

  class Rectangle(val lowerLeft: Point, val height: Double, val width: Double)
        extends Shape {
    def draw() = println("Rectangle.draw: " + this)
    override def toString() = 
      "Rectangle(" + lowerLeft + "," + height + "," + width + ")"
  }

  class Triangle(val point1: Point, val point2: Point, val point3: Point) 
        extends Shape {
    def draw() = println("Triangle.draw: " + this)
    override def toString() = 
      "Triangle(" + point1 + "," + point2 + "," + point3 + ")"
  }
}

形状-actor.scala

package shapes {
  import scala.actors._
  import scala.actors.Actor._

  object ShapeDrawingActor extends Actor {
    def act() {
      loop {
        receive {
          case s: Shape => s.draw()
          case "exit"   => println("exiting..."); exit
          case x: Any   => println("Error: Unknown message! " + x)
        }
      }
    }
  }
}

形状,演员-script.scala

import shapes._

ShapeDrawingActor.start()

ShapeDrawingActor ! new Circle(new Point(0.0,0.0), 1.0)
ShapeDrawingActor ! new Rectangle(new Point(0.0,0.0), 2, 5)
ShapeDrawingActor ! new Triangle(new Point(0.0,0.0),
                                 new Point(1.0,0.0),
                                 new Point(0.0,1.0))
ShapeDrawingActor ! 3.14159

ShapeDrawingActor ! "exit"

现在我尝试从命令行运行以下命令:

scalac shapes.scala shapes-actor.scala
scala -cp . shapes shapes-actor-script.scala

第一个命令将类编译为名为.class的目录中的shapes个文件,但第二个命令引发错误:

/Users/simon/Documents/playground/code-examples/Introdu
cingScala/shapes-actor-script.scala:3: error: not found
: value shapes
import shapes._
       ^
...

我编译/运行程序的方式有问题吗?

1 个答案:

答案 0 :(得分:0)

您似乎将Scala视为它的Python。不幸的是,这不会起作用。我想你误解了什么

% scala -cp . shapes shapes-actor-script.scala

装置。执行该命令时,至少在OS X 10.11.5上执行; Scala 2.11.8; Java 1.8.0_60;然后你得到这个错误:

java.io.IOException: no such file: shapes
    at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:194)
    at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:205)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:67)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) 

它抱怨无法找到shapes,因为我们搞砸了exec语法。请记住语法是scala -cp PATHS CLASS,其中shell将PATHS视为单个参数。所以上面的命令告诉Scala执行类名shapes的主方法,这是我们没有的。

我们可以通过更正命令并使用:加入路径来解决此问题。像这样:

% scala -cp .:shapes shapes-actor-script.scala

现在我们发现了这个错误:

error: error while loading ShapeDrawingActor, class file 'shapes/ShapeDrawingActor.class' has location not matching its contents: contains class ShapeDrawingActor
error: error while loading Circle, class file 'shapes/Circle.class' has location not matching its contents: contains class Circle
error: error while loading Point, class file 'shapes/Point.class' has location not matching its contents: contains class Point
error: error while loading Rectangle, class file 'shapes/Rectangle.class' has location not matching its contents: contains class Rectangle
error: error while loading Triangle, class file 'shapes/Triangle.class' has location not matching its contents: contains class Triangle
5 errors found

这里的问题有两个方面: - 1)Scala将递归加载您在类路径中指定的每个目录的每个类文件。所以我们要加载每个文件。 - 2)scala期望类文件的目录布局与包布局匹配,这意味着如果我们将shapes列为我们唯一的类路径目录,然后尝试加载类shapes.Triangle,那么我们&#39 ;真正尝试查找并加载文件shapes/shapes/Triangle.class。这不存在,因此脚本失败。

让我们从类路径中删除shapes并重试:

% scala -cp . shapes-actor-script.scala
Circle.draw: Circle(Point(0.0,0.0),1.0)
Rectangle.draw: Rectangle(Point(0.0,0.0),2.0,5.0)
Triangle.draw: Triangle(Point(0.0,0.0),Point(1.0,0.0),Point(0.0,1.0))
Error: Unknown message! 3.14159
exiting...

有效!

您实际上还有其他几种方法可以调用此脚本。 scala命令默认会查找来自.的类文件,因此我们可以编写:

% scala shapes-actor-script.scala

如果您想将shapes-actor-script.scala作为真实脚本执行,那么最简单的方法是按照标题Script it!下的说明进行操作。只需添加:

#!/bin/sh
exec scala "$0" "$@"
!#
...rest of shapes-actor-script.scala

然后在命令行上执行它,就像执行任何其他脚本一样(确保文件是可执行的!):

% ./shapes-actor-script.scala

祝你好运学习scala!