Running Scala project whose directory structure does not match packages in source
Hi,
I'm working on a Scala project where the packages declared in the source
file "import ..." statement does not match the directory structure.
The project builds and runs with SBT, but I can't get Eclipse to run the
main class.
However, if I import the project to Eclipse (with sbteclipse), and right
click on main() -> Run As..., I do not get the option to "Run as Scala
Application".
If I add a Run Configuration with main class
"myproject.hello.mainclass.MainClass", I get the error "Cannot locate main
type main type ... For this to work the package name in the source needs to
match the source's physical location."
Is there a way to run the main class without modifying the directory
structure? (I'm working on a large project and don't have the flexibility
to change the directory structure.)
Thanks!
----
Here's an example of the compile/run working in SBT:
*tba:test$* cat build.sbt
name := "A Project"
version := "0.1"
scalaVersion := "2.10.1"
*tba:test$* cat ./src/main/scala/hello/hello.scala
package myproject.hello*.*mainclass // <-- doesn't match the directory
src/main/scala/hello/
object MainClass {
def main( args:Array[String] ) {
System.out.println("hi")
}
}
*tba:test$* sbt "run-main myproject.hello.mainclass.MainClass"
[info] Loading global plugins from /Users/tba/.sbt/plugins
[info] Set current project to A Project (in build
file:/Users/tba/HMC/research/workspace/test/)
[info] Running myproject.hello.mainclass.MainClass
hi
[success] Total time: 0 s, completed Jun 17, 2013 12:16:01 PM
In scala-ide:9f47afbcb5d548b4555b9f33d870bb924c33c202 Tuned ``MainClassVerifier`` to consider classfiles instead of sourcefiles
The Problem
+++++++++++
With the former implementation, an error was reported every time the package
name in the source did not match the source's physical location. This is too
eager, in fact the two don't need to be in sync for the user to be able to
launch a Scala Application.
However, despite the error dialog, the Scala Main class was still run. The
reason for this is that there was a double bug! (I'm gonna name tihs one
*hadouken-bug*) [This
line](https://github.com/scala-ide/scala-ide/pull/444/files#L2L122) was always
returning ``true``. What that line wanted to check is the ``IStatus.severity``
field, but it was actually comparing the ``IStatus.code`` (the IStatus
interface is definitely not the cleanest API ever, can't really blame people
for getting confused...).
The Solution
++++++++++++
The minimal condition for running a Scala Application is that the class' Main
binary exists. Hence, I've updated the code to reflect this requirement.
Moreover, if a class file for the ``mainTypeName`` exist, but no companion
module class file can be found, an error is now reported to inform the user
that he needs to change the class declaration into an object.
Fix #1001760
In scala-ide:b5f4ccfbf5ecb07654c3c06e1b91c70bfdc13752 Tuned ``MainClassVerifier`` to consider classfiles instead of sourcefiles
The Problem
+++++++++++
With the former implementation, an error was reported every time the package
name in the source did not match the source's physical location. This is too
eager, in fact the two don't need to be in sync for the user to be able to
launch a Scala Application.
However, despite the error dialog, the Scala Main class was still run. The
reason for this is that there was a double bug! (I'm gonna name tihs one
*hadouken-bug*) [This
line](https://github.com/scala-ide/scala-ide/pull/444/files#L2L122) was always
returning ``true``. What that line wanted to check is the ``IStatus.severity``
field, but it was actually comparing the ``IStatus.code`` (the IStatus
interface is definitely not the cleanest API ever, can't really blame people
for getting confused...).
The Solution
++++++++++++
The minimal condition for running a Scala Application is that the class' Main
binary exists. Hence, I've updated the code to reflect this requirement.
Moreover, if a class file for the ``mainTypeName`` exist, but no companion
module class file can be found, an error is now reported to inform the user
that he needs to change the class declaration into an object.
Fix #1001760