如何从Scala中的其他目录导入文件?
我有这样的目录层次结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用成熟的构建工具,而不是手忙脚乱地使用 shell 脚本。 SBT 一定是您的最佳选择。
确保每个文件还
导入
它所依赖的类。Use a grown up build tool, instead of messing around with hand-rolled shell scripts. SBT has got to be your best bet here.
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.
Make sure that each file also
import
s classes that it has a dependency on.由于您尚未报告您遇到的错误,因此我们只能猜测。然而,基本错误似乎只是您正在编译在不同步骤中相互引用的代码。解决方案很简单:不要这样做。这样做:
但是,如果您确定不存在交叉依赖关系,则需要将 CLASSES_PATH 作为类路径传递:
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:
If, however, you are sure there are no cross dependencies, you need to pass the CLASSES_PATH as classpath: