Scala 项目不会在 Eclipse 中编译;“无法找到主类。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:40:54  来源:igfitidea点击:

Scala project won't compile in Eclipse; "Could not find the main class."

eclipsescalaeclipse-pluginscala-ide

提问by Thomas Heywood

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().

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

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 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.

Eclipse 正确查找 Main$ 类,而不是
调试配置下的 Main 类*,我的主类被正确标识为 mypackage.Main
* 我的插件是最新的,推荐用于我的 Eclipse 版本
* 清理、重新启动等。没有帮助。

The same project will compile with scalac.

同一个项目将使用 scalac 进行编译。

Thanks for any ideas on how to solve this.

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

EDIT: MatthieuF suggested I should post the code.

编辑:MatthieuF 建议我应该发布代码。

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.

此代码段产生错误。这不是最惯用的代码,但我以这种方式编写它来测试我的环境。我将它作为单个文件和单独的文件进行了尝试。它确实适用于 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
   }
}

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

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

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
    }
}

采纳答案by ninjagecko

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).

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

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.

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

回答by Matthew Farwell

One possibility is that you are trying to launch using ctrl-F11, but from a different class.

一种可能性是您尝试使用 ctrl-F11 启动,但来自不同的类。

The Scala Eclipse plugin does not obey the defaults for Java launching. In Preferences->Run/Debug->Launching, there are some options Launch Operation->Always Launch the previously selected application, etc. This currently does not work in the Scala eclipse plugin. To launch a specified main, you need to launch it from the editor for the class.

Scala Eclipse 插件不遵循 Java 启动的默认设置。在 Preferences->Run/Debug->Launching 中,有一些选项 Launch Operation->Always Launch the previous selected application 等。目前这在 Scala eclipse 插件中不起作用。要启动指定的 main,您需要从类的编辑器中启动它。

There has been a bug raised for this. http://scala-ide.assembla.com/spaces/scala-ide/tickets/1000023-scala-launch--does-not-follow-jdt-behaviour

为此提出了一个错误。http://scala-ide.assembla.com/spaces/scala-ide/tickets/1000023-scala-launch--does-not-follow-jdt-behaviour

EDIT: This is now (mostly) fixed.

编辑:现在(大部分)已修复。

回答by user1459144

For me it was Eclipse specific problem. I noticed that .classfile wasn't built at all. So bindirectory doesn't have compiled classes. When I manually compiled *.scala file using *.sbt and copied it to bindirectory it was working as expected. I tried different tips and tricks and it wasn't worked until I reinstalled Scala plugin in Eclipse.

对我来说,这是 Eclipse 特定的问题。我注意到该.class文件根本没有构建。所以bin目录没有编译类。当我使用 *.sbt 手动编译 *.scala 文件并将其复制到bin目录时,它按预期工作。我尝试了不同的提示和技巧,但直到我在 Eclipse 中重新安装 Scala 插件后才奏效。

回答by Leon Rom

I'd solve similar problem by executig "Project->Clean.." with next automatically building.

我会通过执行“Project->Clean ..”和下一个自动构建来解决类似的问题。

回答by bluish

I had the same error message with a Java application made by myself.

我自己制作的 Java 应用程序也有同样的错误消息。

The problem was that I deleted (though inside Eclipse) a jar that belonged to the Java build path, without deleting it from the Java build path (project's Properties window). When I did it the class could compile and run again.

问题是我删除了(虽然在 Eclipse 中)一个属于Java 构建路径的 jar ,但没有从 Java 构建路径(项目的属性窗口)中删除它。当我这样做时,该类可以再次编译并运行。

回答by azzurolilc

If you are using Intellij, mark directory as source root

如果您使用的是 Intellij,请将目录标记为源根

回答by bugs

Do you have a proper build tool setup? Like sbt have you installed it?

你有合适的构建工具设置吗?像sbt你安装了吗?

You can check its version by $sbt --version

您可以通过 $sbt --version 检查其版本

If it is not setup you can download from here http://www.scala-sbt.org/download.html

如果没有设置,你可以从这里下载http://www.scala-sbt.org/download.html

You might have to restart your eclipse after installation.

您可能需要在安装后重新启动 eclipse。

回答by Ashiq Imran

Just copy your XXX.scala file code. Remove the package and create a new Scala Class. Paste your XXX.scala code. (If you are using maven, do a maven clean and build.) Run configuration again. This works for me.

只需复制您的 XXX.scala 文件代码。删除包并创建一个新的 Scala 类。粘贴您的 XXX.scala 代码。(如果您使用的是 maven,请执行 maven clean 和 build。)再次运行配置。这对我有用。

回答by Ashiq Imran

I have faced this issue. I have just deleted the package name, created scala class, Written the same code, Set Build to "Build Automatically". Finally, It works perfectly fine.

我遇到过这个问题。我刚刚删除了包名,创建了scala类,编写了相同的代码,将构建设置为“自动构建”。最后,它工作得很好。

回答by Daniel Sobrado

Check scala-ide.log

检查 scala-ide.log

For me the issue was that there were errors on:

对我来说,问题是以下方面存在错误:

AppData\Local\Temp\sbt_10d322fb\xsbt\ClassName.scala:16: error: not found: value enteringPhase enteringPhase(currentRun.flattenPhase.next) { s fullName separator }

AppData\Local\Temp\sbt_10d322fb\xsbt\ClassName.scala:16: 错误: 未找到: value enterPhase enterPhase(currentRun.flattenPhase.next) { s fullName separator }