如何从Scala中的其他目录导入文件?

发布于 2024-11-07 04:37:28 字数 1350 浏览 1 评论 0原文

我有这样的目录层次结构:

src
src/Model
src/View
src/Controller

现在我想构建我的应用程序。我如何从模型视图和控制器导入/包含类,因为编译器看不到它们?

// 编辑

src/App.scala

import swing._

object App extends Application {

  val model       = new Model
  val view        = new View(model)
  val controller  = new Controller(model, view)

  view.visible = true

}

src/Model/Model.scala

class Model {
  // some code
}

src/View/View.scala

import swing._

class View(model:Model) extends MainFrame {
  // some code
}

src/Controller/Controller.scala

class Controller(model:Model, view:View) {
  // some code
}

这是一个构建脚本

#!/bin/bash

source ${0%/*}/config.inc.sh

if [ ! -d $CLASSES_PATH ]; then
  notice "Creating classes directory..."
  mkdir $CLASSES_PATH
fi

notice "Building VirtualCut..."
scalac $SOURCE_PATH/Model/*.scala -d $CLASSES_PATH || error "Build failed (Model)."
scalac $SOURCE_PATH/View/*.scala -d $CLASSES_PATH || error "Build failed (View)."
scalac $SOURCE_PATH/Controller/*.scala -d $CLASSES_PATH || error "Build failed (Controller)."
scalac $SOURCE_PATH/*.scala -d $CLASSES_PATH || error "Build failed."
success "Building complete."

exit 0

当所有文件都在 src 目录中时,一切正常。

I have dir hierarchy such as this:

src
src/Model
src/View
src/Controller

Now I want to built my application. How can I import/include classes from Model View and Controller, becouse compiler can't see them?

// edit

src/App.scala

import swing._

object App extends Application {

  val model       = new Model
  val view        = new View(model)
  val controller  = new Controller(model, view)

  view.visible = true

}

src/Model/Model.scala

class Model {
  // some code
}

src/View/View.scala

import swing._

class View(model:Model) extends MainFrame {
  // some code
}

src/Controller/Controller.scala

class Controller(model:Model, view:View) {
  // some code
}

Here is a build script

#!/bin/bash

source ${0%/*}/config.inc.sh

if [ ! -d $CLASSES_PATH ]; then
  notice "Creating classes directory..."
  mkdir $CLASSES_PATH
fi

notice "Building VirtualCut..."
scalac $SOURCE_PATH/Model/*.scala -d $CLASSES_PATH || error "Build failed (Model)."
scalac $SOURCE_PATH/View/*.scala -d $CLASSES_PATH || error "Build failed (View)."
scalac $SOURCE_PATH/Controller/*.scala -d $CLASSES_PATH || error "Build failed (Controller)."
scalac $SOURCE_PATH/*.scala -d $CLASSES_PATH || error "Build failed."
success "Building complete."

exit 0

Everything works fine when all files are in src dir.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-11-14 04:37:28
  1. 使用成熟的构建工具,而不是手忙脚乱地使用 shell 脚本。 SBT 一定是您的最佳选择。

  2. ,指定它应该属于哪个包。仅转储默认包中的所有内容是不明智的 - 这是未来命名空间冲突的保证。

  3. 确保每个文件还导入它所依赖的类。

  1. Use a grown up build tool, instead of messing around with hand-rolled shell scripts. SBT has got to be your best bet here.

  2. At the top of each source file, specify what package it should belong in. It's ill-advised to just dump everything in the default package - a guaranteed recipe for future namespace conflicts.

  3. Make sure that each file also imports classes that it has a dependency on.

桃扇骨 2024-11-14 04:37:28

由于您尚未报告您遇到的错误,因此我们只能猜测。然而,基本错误似乎只是您正在编译在不同步骤中相互引用的代码。解决方案很简单:不要这样做。这样做:

scalac $SOURCE_PATH/Model/*.scala $SOURCE_PATH/View/*.scala $SOURCE_PATH/Controller/*.scala $SOURCE_PATH/*.scala -d $CLASSES_PATH || error "Build failed."
success "Building complete."

但是,如果您确定不存在交叉依赖关系,则需要将 CLASSES_PATH 作为类路径传递:

scalac $SOURCE_PATH/Model/*.scala-d $CLASSES_PATH || error "Build failed (Model)."
scalac $SOURCE_PATH/View/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed (View)."
scalac $SOURCE_PATH/Controller/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed (Controller)."
scalac $SOURCE_PATH/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed."

Since you have not reported what error you are getting, we are left to guess. However, the basic error seems to be simply that you are compiling code that reference each other in different steps. The solution is simple: don't do that. Do this:

scalac $SOURCE_PATH/Model/*.scala $SOURCE_PATH/View/*.scala $SOURCE_PATH/Controller/*.scala $SOURCE_PATH/*.scala -d $CLASSES_PATH || error "Build failed."
success "Building complete."

If, however, you are sure there are no cross dependencies, you need to pass the CLASSES_PATH as classpath:

scalac $SOURCE_PATH/Model/*.scala-d $CLASSES_PATH || error "Build failed (Model)."
scalac $SOURCE_PATH/View/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed (View)."
scalac $SOURCE_PATH/Controller/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed (Controller)."
scalac $SOURCE_PATH/*.scala -cp $CLASSES_PATH -d $CLASSES_PATH || error "Build failed."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文