java.lang.NoSuchMethodError: main 在使用 Eclipse Scala 插件启动 HelloWorld 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135731/
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-19 13:53:26  来源:igfitidea点击:

java.lang.NoSuchMethodError: main when starting HelloWorld with Eclipse Scala plugin

eclipsescala

提问by Matt Sheppard

I've just been playing with Scala, and installed the Eclipse plugin as described at http://www.scala-lang.org/node/94, but after entering the "Hello World" test example and setting up the run configuration as described, I get the following error

我刚刚在玩 Scala,并按照http://www.scala-lang.org/node/94 中的描述安装了 Eclipse 插件,但是在进入“Hello World”测试示例并将运行配置设置为描述,我收到以下错误

Exception in thread "main" java.lang.NoSuchMethodError: main

For reference the code is

供参考的代码是

package hello

object HelloWorld extends Application {
  println("Hello World!")
}

I've tinkered a bit with the obvious solutions (adding a main method, adding a singleton object with a main method) but I'm clearly doing something wrong.

我对明显的解决方案做了一些修改(添加一个主要方法,用一个主要方法添加一个单例对象),但我显然做错了。

Can anyone get their test example to work, or point out what I am doing wrong?

任何人都可以让他们的测试示例工作,或者指出我做错了什么?

回答by marc esher

I hit the same issue last night. I fixed it by a) removing any existing scala run configurations and then by (I'm not kidding) adding a linebreak before the first curly brace.

我昨晚遇到了同样的问题。我通过以下方式修复了它:a) 删除任何现有的 Scala 运行配置,然后(我不是在开玩笑)在第一个花括号之前添加换行符。

Another thing I did that works is to go into the existing run configuration and add some junk into the "arguments" tab. I'm ignoring the arguments anyway, so it didn't affect the output of the program, but it got the plugin to find main again

我做的另一件事是进入现有的运行配置并将一些垃圾添加到“参数”选项卡中。反正我忽略了参数,所以它没有影响程序的输出,但是它让插件再次找到 main

回答by brent.payne

I also hit this error with the below code:

我也用下面的代码遇到了这个错误:

package hello

class HelloWorld extends Application {
  Console.println("Hello World!")
}

The error was using classinstead of object, when I switched to objectit ran fine in Eclipse.

错误是使用class而不是object,当我切换到object它时,它在 Eclipse 中运行良好。

回答by Leon Chen

I got the same issue. Here is how I fixed it: Right click on your project, select "properties->Java Build Path", you can see the "Default output folder" is "your project/bin". Change it to "your project/classes", then clean and rebuild your project. A few complains might still show up, but it will get through - just make sure the compiled ".class" files are generated under "classes" folder

我遇到了同样的问题。这是我修复它的方法:右键单击您的项目,选择“属性->Java 构建路径”,您可以看到“默认输出文件夹”是“您的项目/ bin”。将其更改为“您的项目/”,然后清理并重建您的项目。一些抱怨可能仍会出现,但它会通过 - 只需确保已编译的“.class”文件在“classes”文件夹下生成

I suspect it has something to do with the underlying Java's execution engine/configuration. Not quite sure because I'm not familier with Java.

我怀疑它与底层 Java 的执行引擎/配置有关。不太确定,因为我不熟悉 Java。

回答by Alexander Stolz

I solve the problem by cleaning the project, then going to the class with the main method and building it with strg+s (auto build on). Works like a charm.

我通过清理项目来解决这个问题,然后使用 main 方法转到类并使用 strg+s(自动构建)构建它。奇迹般有效。

回答by Alexander Stolz

It is a little bit ugly, but you can place

有点难看,不过可以放

package hello

object HelloWorld {
  def main(args:Array[String]) {
    println("Hello World!")
  }
}

inside a File HelloWordSrc.scala (or whatever you like instead of the trailing "Src"), maybe clean the project once, and then Eclipse should run it. The "extends Application" did never work for me inside Eclipse, but the plugin looks for a main method inside the code, which for some reason cannot have the same name as designated by the file's name

在文件 HelloWordSrc.scala(或任何您喜欢的文件而不是尾随的“Src”)中,可能会清理项目一次,然后 Eclipse 应该运行它。“扩展应用程序”在 Eclipse 中对我来说从来没有用过,但是插件在代码中寻找一个 main 方法,由于某种原因,它不能与文件名指定的名称相同

cheers

干杯

回答by 01es

Had exactly the same problem. The solution for me was this:

有完全相同的问题。我的解决方案是这样的:

  • Go to Run->Run Configuration
  • Create new JavaApplication configuration where manually specify the full class name (e.g. hello.HelloWorld)
  • Apply and run
  • 转到运行->运行配置
  • 创建新的 JavaApplication 配置,手动指定完整的类名(例如 hello.HelloWorld)
  • 应用并运行

That should be it.

应该是这样。

EDIT

编辑

As to the cause of the problem, IMHO Eclipse is looking for file HelloWorld.class to be executed. However, what it should be looking for is HelloWorld$.class (or the other way around). But this assumption could be wrong...

至于问题的原因,恕我直言,Eclipse 正在寻找要执行的文件 HelloWorld.class。然而,它应该寻找的是 HelloWorld$.class (或相反)。但是这个假设可能是错误的......

回答by starblue

The Scala plugin is a bit unstable. Try closing and reopening the project, that usually helps.

Scala 插件有点不稳定。尝试关闭并重新打开项目,这通常会有所帮助。

回答by Daniel C. Sobral

Unfortunately, you did nothing wrong. Scala's Eclipse plugin has seen better days, but it has been rewritten to take advantage of some recent features enabling better integration, and the present version is buggy.

不幸的是,你没有做错任何事。Scala 的 Eclipse 插件已经有了更好的日子,但它已被重写以利用一些最近的特性来实现更好的集成,并且当前版本有缺陷。

Right now, I recommend NetBeans. Or IDEA, if you've got the money.

现在,我推荐 NetBeans。或者 IDEA,如果你有钱的话。

回答by Antony Stubbs

Netbeans works ~ok but also has problems. Right now I have a good Eclipse 3.4 + Scala Plugin 2.7.5 Final working very well. I suggest you do a clean install of Eclipse 3.4, Scala 2.7.5, clean your project etc. Did you create the project with the Scala project creator? i.e. Create new project -> Scala project? All this is of course if nothing else is working for you.

Netbeans 工作正常,但也有问题。现在我有一个很好的 Eclipse 3.4 + Scala Plugin 2.7.5 Final 工作得很好。我建议您全新安装 Eclipse 3.4、Scala 2.7.5,清理您的项目等。您是否使用 Scala 项目创建者创建了该项目?即创建新项目 -> Scala 项目?当然,如果没有其他方法对您有用的话,这一切都是理所当然的。

回答by user unknown

If you use a recent version of scala (2.9.*), you should use

如果您使用最新版本的 Scala (2.9.*),您应该使用

object HelloWorld extends App {
  println("Hello World!")
}

App, not Application. Application was the 2.8 or 2.7 way. Whether this is the only problem is another question.

应用程序,而不是应用程序。应用程序是 2.8 或 2.7 方式。这是否是唯一的问题是另一个问题。