简体   繁体   English

在 STS/Eclipse 中找不到或加载主 class

[英]Could not find or load main class in STS/Eclipse

I have a main method in a package in one of my projects.我的一个项目中的 package 中有一个主要方法。 Say, the package is com.ant.car .比如说, package 是com.ant.car I am trying to run and/or debug this main method, and I keep getting the error Could not find or load main class com.ant.car .我正在尝试运行和/或调试此主要方法,但我不断收到错误Could not find or load main class com.ant.car

I've searched this problem, and it seems like I can't figure out what is wrong.我已经搜索过这个问题,似乎我无法弄清楚出了什么问题。

1) I've checked run configurations. 1)我检查了运行配置。 In Run->Run Configurations, I've checked that the Main class is com.ant.car.在 Run->Run Configurations 中,我检查了 Main class 是 com.ant.car。

2) I've checked build path. 2)我检查了构建路径。 If I right click on the project, I select Build->Build Path, and under the Libraries tab I make sure there are no missing folders with red Xs next to them.如果我右键单击该项目,我选择 select Build->Build Path,并在 Libraries 选项卡下确保没有丢失的文件夹旁边有红色 X。

Not really sure what else to do.不太确定还能做什么。 Any suggestions?有什么建议么?

Project -> Clean项目 -> 清洁

this is working这是工作

Try updating project by right clicking on it.尝试通过右键单击来更新项目。 Maven -> Update Project Maven -> 更新项目

Today I ran into the same problem and I tried a lot of answers.今天我遇到了同样的问题,我尝试了很多答案。 Nothing helped.没有任何帮助。 Cleaning the project, build automatically is already checked, deleting .metadata , etc.清理项目,自动构建已经被选中,删除.metadata等。

Eventually I tried this and it worked perfectly:最终我尝试了这个,它工作得很好:

menu Project -> Properties菜单项目 -> 属性
Java Build Path, tab Libraries Java 构建路径,选项卡库

Remove the JRE System Library from there, press Add Library , take the JRE System Library and press Next .从那里删除 JRE 系统库,按Add Library ,获取 JRE System Library 并按Next Workspace default JRE and press Finish .工作区默认JRE并按Finish

What worked for me: Menu Project -> Properties In Java Build Path , tab Libraries Delete all libraries with a red [x] next to them.对我有用的是: Menu Project -> Properties In Java Build Path ,选项卡Libraries删除旁边带有红色 [x] 的所有库。

In my case, problem happened when I switched from Kepler to STS IDE.就我而言,当我从 Kepler 切换到 STS IDE 时发生了问题。

The solution to this was the following:解决方案如下:

  • Close Eclipse/STS关闭 Eclipse/STS
  • Use a file explorer on your operating system to navigate to your workspace (In my case, I'm on Windows so I used Windows Explorer)使用操作系统上的文件资源管理器导航到您的工作区(就我而言,我使用的是 Windows,所以我使用了 Windows 资源管理器)
  • Delete the .metadata directory (or to be safe, copy the directory somewhere else to be safe, then delete it)删除.metadata目录(或者为了安全起见,将目录复制到其他地方以确保安全,然后将其删除)
  • Restart Eclipse/STS重启 Eclipse/STS

Is there a more improved answer than this?还有比这更好的答案吗? I don't want to look like I'm trying to boost my own reputation points, so if someone can provide a better answer then please do so.我不想看起来像我在努力提高自己的声誉点,所以如果有人可以提供更好的答案,那么请这样做。

Sometimes the problem can be caused by some newly added dependencies.有时问题可能是由一些新添加的依赖项引起的。 Try removing the recently added jars.尝试删除最近添加的罐子。 That's how I fixed my problem.这就是我解决我的问题的方法。

This issue occurs when the main .class file moved or not found because you changed the directory for committed/shared the project into the git or another repository.当主 .class 文件移动或未找到时会出现此问题,因为您将项目的提交/共享目录更改为 git 或其他存储库。

To Resolve this issue --> Remove existing run configuration and new one.解决这个问题 --> 删除现有的运行配置和新的。 Find the parent pom.xml or project pom.xml and open cmd/command prompt and run the below commands,找到父 pom.xml 或项目 pom.xml 并打开 cmd/命令提示符并运行以下命令,

  1. mvn clean install package mvn 全新安装包
  2. mvn eclipse eclipse mvn 日食

It worked for me:它对我有用:
1. Delete metadata from work-space directory. 1. 从工作空间目录中删除元数据。
2. Import the project again, but selected copy to work-space option. 2. 再次导入项目,但选择复制到工作区选项。

I think the cause for the error was Non-English characters in the original saved directory.我认为错误的原因是原始保存目录中的非英文字符。

您只需要删除运行配置文件并重新启动 STS 并运行命令 -mvn clean install 并尝试重新启动服务

The Error "Could not find or load main class com.ant.car" occur when your class is not in build path.当您的类不在构建路径中时,会发生错误“无法找到或加载主类 com.ant.car”。 please make sure that .class file is generated and it's in build path ..请确保生成了 .class 文件并且它在构建路径中..

I faced the same issue..我遇到了同样的问题..

just do follow these steps:只需按照以下步骤操作:

STS/Eclipse --> Project --> Enable "Build Automatically" STS/Eclipse --> 项目 --> 启用“自动构建”

then refresh your project, it will resolve your issue.然后刷新您的项目,它将解决您的问题。

Still not refreshed your projects automatically, just restart your STS and check.仍然没有自动刷新您的项目,只需重新启动您的 STS 并检查。

Hope it will help you.希望它会帮助你。

I think .class files are deleted/missing from JavaProject/bin folder.我认为 .class 文件已从 JavaProject/bin 文件夹中删除/丢失。

To resolve this issue ->要解决此问题->

1) Just cut paste and save the code contents of all the files that you are using then .class files will be regenerated. 1) 只需剪切粘贴并保存您正在使用的所有文件的代码内容,即可重新生成 .class 文件。

2) Then run the code and you can see it works fine if there is no syntactical errors. 2)然后运行代码,如果没有语法错误,您可以看到它工作正常。

Project -> Clean... -> check project not working -> Clean项目 -> 清理... -> 检查项目不工作 -> 清理

I already had build automatically set, but forcing STS to rebuild it fixed it.我已经自动设置了构建,但是强制 STS 重建它修复了它。

1 ) Clean the Project 1)清理项目
2 ) Enable build automatically Option 2 ) 启用自动构建选项
3 ) Update the maven project by use the short cut Alt + F5 3 ) 使用快捷键 Alt + F5更新 maven 项目

手动删除目标文件夹和 mvn clean install 对我有用

A quick and easy fix is to directly run your SpringBootApplication class (ie Right click, Run As -> Spring Boot App).一个快速而简单的解决方法是直接运行您的 SpringBootApplication 类(即右键单击,Run As -> Spring Boot App)。 This runs the app and creates a run configuration automatically.这将运行应用程序并自动创建运行配置。

You can try one of the two solutions below: Solution 1 : Right click the project -> Gradle -> Refresh Gradle Project.您可以尝试以下两种解决方案之一:解决方案 1 :右键单击项目 -> Gradle -> 刷新 Gradle 项目。

Solution 2 : Add gradle or maven in your environment variable.解决方案 2 :在您的环境变量中添加 gradle 或 maven。 My problem was that I had not added gradle in my environment variable.我的问题是我没有在我的环境变量中添加 gradle。 Before, make sure Gradle or maven is installed in your OS.之前,请确保您的操作系统中安装了 Gradle 或 maven。

我在这个问题上花了几个小时,最后通过这样做来修复它:属性 - > Java 编译器:取消选中复选框“使用'--release'选项”

This worked for me to solve the error.这对我来说解决了错误。 (I got this error after removing AWS ToolKit) (删除 AWS ToolKit 后出现此错误)

  1. Close the Eclipse/ STS Eclipse.关闭Eclipse/STS Eclipse。
  2. Go to the WorkSpace folder.转至工作区文件夹。
  3. Delete the .metadata folder.删除.metadata文件夹。
  4. Open the eclipse.打开日食。
  5. Run maven install on pom.在 pom 上运行maven install
  6. Run the project with your Run configuration.使用您的运行配置运行项目。
  1. Remove project from STS/Eclipse从 STS/Eclipse 中删除项目
  2. Close or Refresh the Eclipse/ STS Eclipse.关闭或刷新 Eclipse/STS Eclipse。
  3. Run maven install on pom.在 pom 上运行 maven install。
  4. Run the project with your Run configuration.使用您的运行配置运行项目。 Tried above mentioned steps to resolved issue.尝试了上述步骤来解决问题。

I tried all the answers but finally what worked for me was deleting the project from eclipse workspace and importing it again.我尝试了所有答案,但最终对我有用的是从 eclipse 工作区中删除项目并再次导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM