Scala 项目不会在 Eclipse 中编译;“找不到主类."

2022-01-16 00:00:00 scala eclipse-plugin eclipse scala-ide

我已经从/update-current 安装了 Eclipse 3.5.2 和今天的 Scala 插件(即 Scala 2.8 final).我可以编译和运行由实现 main() 的单个单例对象组成的 Scala 项目.

I have installed Eclipse 3.5.2 and today's Scala plugin from /update-current (that's Scala 2.8 final.) I can compile and run Scala projects consisting of a single singleton object that implements main().

但是,如果项目包含更多类,我会收到找不到主类"错误.

But, if a project contains more classes, I receive the "Could not find the main class" error.

我已经尝试搜索解决方案,我发现:

I have tried searching for the solution and I discovered:

Eclipse 正在正确查找 Main$ 类,而不是 Main 类
* 在调试配置下,我的主类被正确识别为 mypackage.Main
* 我的插件是最新的,推荐用于我的 Eclipse 版本
* 清理、重启等无济于事.

Eclipse is correctly looking for the Main$ class, not the Main class
* under Debug Configurations, my main class is correctly identified as mypackage.Main
* my plugin is up to date and recommended for my version of Eclipse
* cleaning, restarting etc. doesn't help.

同一个项目将使用 scalac 编译.

The same project will compile with scalac.

感谢您提供有关如何解决此问题的任何想法.

Thanks for any ideas on how to solve this.

MatthieuF 建议我应该发布代码.

MatthieuF suggested I should post the code.

此代码段会产生错误.这不是最惯用的代码,但我这样写是为了测试我的环境.我将其作为单个文件和单独的文件进行了尝试.它确实适用于 scalac.

This snippet produces an error. It's not the most idiomatic code, but I wrote it that way to test my environment. I tried it as a single file and as separate files. It DOES work with scalac.

import swing._

class HelloFrame extends Frame {
        title = "First program"
        contents = new Label("Hello, world!")
}

object Hello {
  val frame = new HelloFrame    
  def main(args : Array[String]) : Unit = {
        frame.visible = true
   }
}

但是,如果我将 HelloFrame 的定义嵌套在 Hello 中,它就可以工作.此代码段完美运行:

BUT, if I nest the definition of HelloFrame within Hello, it works. This snippet runs perfectly:

import swing._

object Hello {

    class HelloFrame extends Frame {
        title = "First program"
        contents = new Label("Hello, world!")
    }

    val frame = new HelloFrame

    def main(args : Array[String]) : Unit = {
        frame.visible = true
    }
}

推荐答案

对我来说,问题是存在阻止编译的构建错误(请参阅问题"选项卡);哎呀!您看到错误的原因是尽管编译步骤失败,运行宏仍在继续,并尝试运行它期望存在的类文件;它们不存在,因为存在阻止编译的构建错误,所以它说它找不到 Main(未编译).

For me, the problem was that there was a build error (see Problems tab) which was preventing compilation; oops! The reason you see the error is that the run macro proceeds despite the failed compilation step, and attempts to run class files it expects to be there; they don't exist because there was a build error preventing compilation, so it says it can't find Main (not compiled).

当构建成功完成时问题就消失了,即错误得到修复.

Problem goes away when build can complete successfully, i.e. errors are fixed.

我想,理论上,您的构建未成功完成可能有更复杂的原因,这些原因未在问题中列出.

I guess, theoretically, there may be more complicated reasons your build is not completing successfully that are not listed in Problems.

相关文章