无法在 Eclipse 中作为 Java 应用程序运行,即使有正确的 main() 类

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

Cannot run as java application in Eclipse, even have correct main() class

javaeclipsem2eclipse

提问by user1314404

I get confusing with Eclipse, when I import a project from Git, then I click on a java file, which has correct main function like:

当我从 Git 导入一个项目时,我对 Eclipse 感到困惑,然后我单击了一个 java 文件,该文件具有正确的主要功能,例如:

  public static void main(String[] args) throws Exception { }

and the outside class has same name with java file. But when I right click the java file, only have "run on server" or "run configuration" option, no "run as java application" like normally. Even if I copy a java file which I can run as java application from different project to this project (no error) then it do not have this option ??? I am quite sure it is nothing to do with the code. I checked the code, totally the same and should be able to run.(I am aware of similar questions on the website but I checked and my problem is not there)

并且外部类与java文件同名。但是当我右键单击 java 文件时,只有“在服务器上运行”或“运行配置”选项,没有像通常那样“作为 java 应用程序运行”。即使我将一个可以作为 java 应用程序从不同项目运行的 java 文件复制到这个项目(没有错误),那么它也没有这个选项???我很确定这与代码无关。我检查了代码,完全一样,应该可以运行。(我知道网站上有类似的问题,但我检查过,我的问题不存在)

So is there any extra steps I need to do with this project (like maven build ? -> I tried this but also can't run, how to run it properly ?) to have this option again ???

那么我需要对这个项目做任何额外的步骤吗(比如 maven build?-> 我试过这个但也不能运行,如何正确运行它?)再次拥有这个选项???

Really confusing !

真的很纠结!

采纳答案by Fred Bricon

If the project is already imported in your workspace, Right click on your and Configure > Convert to Maven... This will enable the Maven builder and force m2e configurators to setup all the required project natures on your project.

如果项目已导入您的工作区,请右键单击您的配置 > 转换为 Maven... 这将启用 Maven 构建器并强制 m2e 配置器在您的项目中设置所有必需的项目性质。

You can also install the m2eclipse-git connector to streamline the "import maven project from git" process. It should be available from Preferences > Maven > Discovery > Open Catalog.

您还可以安装 m2eclipse-git 连接器来简化“从 git 导入 maven 项目”过程。它应该可以从 Preferences > Maven > Discovery > Open Catalog 获得。

Once it's done, you should import your project via Import...> Maven > Checkout Maven Projects from SCM

完成后,您应该通过 Import...> Maven > Checkout Maven Projects from SCM 导入您的项目

回答by Tassos Bassoukos

Keep in mind that projects imported from GIT are not necessarily javaprojects, as Eclipse understands this term. Is the project, as it exists in the repository, an Eclipse project? If it is, it should have two files in its root folder named .classpathand .project. If these two files are missing, you will need to instruct Eclipse to create them, with logical defaults. Also, if the root folder of the project has a pom.xmlfile, then it is a maven project, which is a whole different kettle of fish.

请记住,从 GIT 导入的项目不一定是java项目,因为 Eclipse 理解这个术语。该项目是否存在于存储库中,是一个 Eclipse 项目?如果是,它的根文件夹中应该有两个名为.classpath和 的文件.project。如果缺少这两个文件,您将需要指示 Eclipse 使用逻辑默认值创建它们。还有,如果项目的根文件夹有一个pom.xml文件,那就是maven项目,完全不同的鱼缸。

回答by websoft102030

This can seem obvious but check if the function main is correctly declared. Function's signature must be :

这看起来很明显,但请检查函数 main 是否正确声明。函数的签名必须是:

static void main (String args[])

If you miss this point (ex: you forget scale declaration for args with []) you won't be able to use the option "run as appication", in my case option wasn't displayed.

如果您错过了这一点(例如:您忘记了带有 [] 的 args 的比例声明),您将无法使用“作为应用程序运行”选项,在我的情况下,该选项未显示。

回答by user3915489

I was facing a similar issue. Below steps solved the problem:

我面临着类似的问题。以下步骤解决了问题:

  1. Import project from git
  2. Right click on Project -> Import -> Import as existing Maven Project
  3. One maven module will be imported, you can open .java file with main method from that module
  4. Right click, run as -> you can see the option.
  1. 从 git 导入项目
  2. 右键单击项目 -> 导入 -> 导入为现有的 Maven 项目
  3. 将导入一个 Maven 模块,您可以使用该模块的 main 方法打开 .java 文件
  4. 右键单击,运行方式 -> 您可以看到该选项。

回答by Vidhya28

If you are handling multi-module project, you might be trying to execute the java file( main class) within the parent project itself. That would be the mistake.

如果您正在处理多模块项目,您可能会尝试在父项目本身中执行 java 文件(主类)。那将是错误的。

Make sure to open the sub- module ( would be listed as separate project in the explorer) and then execute the java file.

确保打开子模块(将在资源管理器中列为单独的项目),然后执行 java 文件。

回答by Vidhya28

Sometimes it might happen when the maven project in not updated on importing to the workspace.

有时,当 Maven 项目在导入到工作区时未更新时,可能会发生这种情况。

Right click on Project -> Maven -> Update Project..

右键单击项目 -> Maven -> 更新项目..

Then Run As -> Java Application -> Select class containing main method.

然后 Run As -> Java Application -> 选择包含 main 方法的类。